Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
SettingObserver
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
5 / 5
9
100.00% covered (success)
100.00%
1 / 1
 created
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
2
 updated
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 deleted
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
2
 isGlobalSetting
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 getCacheInvalidationService
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace App\Observers;
4
5use App\Http\Models\Setting;
6use App\Http\Services\CacheInvalidationService;
7use App\Jobs\ProcessSettingAsyncJob;
8use Illuminate\Support\Facades\Log;
9
10/**
11 * Observer for Setting model changes.
12 *
13 * Handles async processing and cache invalidation for settings,
14 * including global settings cache management.
15 */
16class SettingObserver
17{
18    /**
19     * Handle the Setting "created" event.
20     */
21    public function created(Setting $setting): void
22    {
23        ProcessSettingAsyncJob::dispatch($setting, 'created');
24
25        // Invalidate global settings cache if this is a global setting
26        if ($this->isGlobalSetting($setting)) {
27            $this->getCacheInvalidationService()->invalidateGlobalSettingsCaches();
28
29            Log::info('Global settings cache invalidated on create', [
30                'setting_id' => (string) $setting->_id,
31            ]);
32        }
33    }
34
35    /**
36     * Handle the Setting "updated" event.
37     */
38    public function updated(Setting $setting): void
39    {
40        ProcessSettingAsyncJob::dispatch($setting, 'updated');
41
42        // Invalidate global settings cache if this is a global setting
43        if ($this->isGlobalSetting($setting)) {
44            $this->getCacheInvalidationService()->invalidateGlobalSettingsCaches();
45
46            Log::info('Global settings cache invalidated on update', [
47                'setting_id' => (string) $setting->_id,
48                'changes' => $setting->getChanges(),
49            ]);
50        }
51    }
52
53    /**
54     * Handle the Setting "deleted" event.
55     */
56    public function deleted(Setting $setting): void
57    {
58        // Invalidate global settings cache if this is a global setting
59        if ($this->isGlobalSetting($setting)) {
60            $this->getCacheInvalidationService()->invalidateGlobalSettingsCaches();
61
62            Log::info('Global settings cache invalidated on delete', [
63                'setting_id' => (string) $setting->_id,
64            ]);
65        }
66    }
67
68    /**
69     * Check if the setting is a global setting.
70     */
71    private function isGlobalSetting(Setting $setting): bool
72    {
73        return $setting->is_global === true
74            || $setting->user_id === Setting::GLOBAL_USER_ID;
75    }
76
77    /**
78     * Get the CacheInvalidationService instance.
79     */
80    private function getCacheInvalidationService(): CacheInvalidationService
81    {
82        return app(CacheInvalidationService::class);
83    }
84}