Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 21 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| SettingObserver | |
0.00% |
0 / 21 |
|
0.00% |
0 / 5 |
90 | |
0.00% |
0 / 1 |
| created | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
| updated | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
| deleted | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| isGlobalSetting | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| getCacheInvalidationService | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Observers; |
| 4 | |
| 5 | use App\Http\Models\Setting; |
| 6 | use App\Http\Services\CacheInvalidationService; |
| 7 | use App\Jobs\ProcessSettingAsyncJob; |
| 8 | use 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 | */ |
| 16 | class 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 | } |