Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
65.22% |
15 / 23 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
| FeatureObserver | |
65.22% |
15 / 23 |
|
75.00% |
3 / 4 |
4.67 | |
0.00% |
0 / 1 |
| created | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| updated | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
| deleted | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
| getCacheInvalidationService | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Observers; |
| 4 | |
| 5 | use App\Http\Models\Feature; |
| 6 | use App\Http\Services\CacheInvalidationService; |
| 7 | use Illuminate\Support\Facades\Log; |
| 8 | |
| 9 | /** |
| 10 | * Observer for Feature model changes. |
| 11 | * |
| 12 | * Handles cache invalidation when features are created, updated, or deleted. |
| 13 | * Also cascades invalidation to all plans using the feature and their subscribers. |
| 14 | */ |
| 15 | class FeatureObserver |
| 16 | { |
| 17 | /** |
| 18 | * Cache key for all features. |
| 19 | */ |
| 20 | private const CACHE_KEY_ALL = 'features:all'; |
| 21 | |
| 22 | /** |
| 23 | * Handle the Feature "created" event. |
| 24 | */ |
| 25 | public function created(Feature $feature): void |
| 26 | { |
| 27 | $this->getCacheInvalidationService()->invalidateFeatureCaches(); |
| 28 | |
| 29 | Log::info('Feature created', [ |
| 30 | 'feature_id' => (string) $feature->_id, |
| 31 | 'key' => $feature->key, |
| 32 | ]); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * Handle the Feature "updated" event. |
| 37 | */ |
| 38 | public function updated(Feature $feature): void |
| 39 | { |
| 40 | $this->getCacheInvalidationService()->invalidateFeatureCaches(); |
| 41 | |
| 42 | // Cascade invalidation to all plans that use this feature |
| 43 | // and their subscribers |
| 44 | $this->getCacheInvalidationService()->invalidateUserSubscriptionCachesByFeature( |
| 45 | (string) $feature->_id |
| 46 | ); |
| 47 | |
| 48 | Log::info('Feature updated', [ |
| 49 | 'feature_id' => (string) $feature->_id, |
| 50 | 'key' => $feature->key, |
| 51 | 'changes' => $feature->getChanges(), |
| 52 | ]); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Handle the Feature "deleted" event. |
| 57 | */ |
| 58 | public function deleted(Feature $feature): void |
| 59 | { |
| 60 | $this->getCacheInvalidationService()->invalidateFeatureCaches(); |
| 61 | |
| 62 | // Cascade invalidation to all plans that used this feature |
| 63 | // and their subscribers |
| 64 | $this->getCacheInvalidationService()->invalidateUserSubscriptionCachesByFeature( |
| 65 | (string) $feature->_id |
| 66 | ); |
| 67 | |
| 68 | Log::info('Feature deleted', [ |
| 69 | 'feature_id' => (string) $feature->_id, |
| 70 | 'key' => $feature->key, |
| 71 | ]); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Get the CacheInvalidationService instance. |
| 76 | */ |
| 77 | private function getCacheInvalidationService(): CacheInvalidationService |
| 78 | { |
| 79 | return app(CacheInvalidationService::class); |
| 80 | } |
| 81 | } |