Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
75.00% |
9 / 12 |
|
33.33% |
1 / 3 |
CRAP | |
0.00% |
0 / 1 |
| SocialSetting | |
75.00% |
9 / 12 |
|
33.33% |
1 / 3 |
3.14 | |
0.00% |
0 / 1 |
| getSetting | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
| setSetting | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| boot | |
71.43% |
5 / 7 |
|
0.00% |
0 / 1 |
1.02 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Models; |
| 4 | |
| 5 | use App\Service\CacheService; |
| 6 | use Illuminate\Database\Eloquent\Model; |
| 7 | |
| 8 | class SocialSetting extends Model |
| 9 | { |
| 10 | protected $table = 'social_settings'; |
| 11 | |
| 12 | protected $fillable = ['key', 'value']; |
| 13 | |
| 14 | public static function getSetting(string $key, $default = null) |
| 15 | { |
| 16 | // CACHED: this used to run one query per call, and it's called from |
| 17 | // several places per request (storefront pixel/whatsapp settings, |
| 18 | // the Facebook/WhatsApp/AI chat services, the chatbot webhook). The |
| 19 | // whole table is small, so fetch it once as a key->value map and |
| 20 | // look up in memory here. Busted immediately by boot() below. |
| 21 | $all = CacheService::remember(CacheService::KEY_SOCIAL_SETTINGS, function () { |
| 22 | return static::query()->pluck('value', 'key'); |
| 23 | }); |
| 24 | |
| 25 | return $all[$key] ?? $default; |
| 26 | } |
| 27 | |
| 28 | public static function setSetting(string $key, $value) |
| 29 | { |
| 30 | return static::updateOrCreate(['key' => $key], ['value' => $value]); |
| 31 | } |
| 32 | |
| 33 | // CACHING: social_setting() helper (MhHelper.php) caches every row of |
| 34 | // this table as one key->value map. updateOrCreate() above still fires |
| 35 | // Eloquent's saved event per row, so busting it here covers both |
| 36 | // setSetting() and any other direct save/update/delete on this model. |
| 37 | protected static function boot() |
| 38 | { |
| 39 | parent::boot(); |
| 40 | |
| 41 | static::saved(function () { |
| 42 | CacheService::forgetSocialSettings(); |
| 43 | }); |
| 44 | |
| 45 | static::deleted(function () { |
| 46 | CacheService::forgetSocialSettings(); |
| 47 | }); |
| 48 | } |
| 49 | } |