Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 30 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| ParameterObserver | |
0.00% |
0 / 30 |
|
0.00% |
0 / 4 |
42 | |
0.00% |
0 / 1 |
| created | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
| updated | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
12 | |||
| deleted | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
| getCacheInvalidationService | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Observers; |
| 4 | |
| 5 | use App\Http\Models\Parameter; |
| 6 | use App\Http\Services\CacheInvalidationService; |
| 7 | use Illuminate\Support\Facades\Log; |
| 8 | |
| 9 | /** |
| 10 | * Observer for Parameter model changes. |
| 11 | * |
| 12 | * Handles cache invalidation when parameters are created, updated, or deleted. |
| 13 | */ |
| 14 | class ParameterObserver |
| 15 | { |
| 16 | /** |
| 17 | * Handle the Parameter "created" event. |
| 18 | */ |
| 19 | public function created(Parameter $parameter): void |
| 20 | { |
| 21 | $this->getCacheInvalidationService()->invalidateParameterCaches( |
| 22 | $parameter->name, |
| 23 | (string) $parameter->_id |
| 24 | ); |
| 25 | |
| 26 | Log::info('Parameter created', [ |
| 27 | 'parameter_id' => (string) $parameter->_id, |
| 28 | 'name' => $parameter->name, |
| 29 | ]); |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * Handle the Parameter "updated" event. |
| 34 | */ |
| 35 | public function updated(Parameter $parameter): void |
| 36 | { |
| 37 | // Get the original name if it changed |
| 38 | $originalName = $parameter->getOriginal('name'); |
| 39 | $currentName = $parameter->name; |
| 40 | |
| 41 | $this->getCacheInvalidationService()->invalidateParameterCaches( |
| 42 | $currentName, |
| 43 | (string) $parameter->_id |
| 44 | ); |
| 45 | |
| 46 | // Also invalidate old name cache if name changed |
| 47 | if ($originalName && $originalName !== $currentName) { |
| 48 | $this->getCacheInvalidationService()->invalidateParameterCaches($originalName); |
| 49 | } |
| 50 | |
| 51 | Log::info('Parameter updated', [ |
| 52 | 'parameter_id' => (string) $parameter->_id, |
| 53 | 'name' => $parameter->name, |
| 54 | 'changes' => $parameter->getChanges(), |
| 55 | ]); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Handle the Parameter "deleted" event. |
| 60 | */ |
| 61 | public function deleted(Parameter $parameter): void |
| 62 | { |
| 63 | $this->getCacheInvalidationService()->invalidateParameterCaches( |
| 64 | $parameter->name, |
| 65 | (string) $parameter->_id |
| 66 | ); |
| 67 | |
| 68 | Log::info('Parameter deleted', [ |
| 69 | 'parameter_id' => (string) $parameter->_id, |
| 70 | 'name' => $parameter->name, |
| 71 | ]); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Get the CacheInvalidationService instance. |
| 76 | */ |
| 77 | private function getCacheInvalidationService(): CacheInvalidationService |
| 78 | { |
| 79 | return app(CacheInvalidationService::class); |
| 80 | } |
| 81 | } |