Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
84.38% covered (warning)
84.38%
27 / 32
80.00% covered (warning)
80.00%
4 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
PlanObserver
84.38% covered (warning)
84.38%
27 / 32
80.00% covered (warning)
80.00%
4 / 5
7.19
0.00% covered (danger)
0.00%
0 / 1
 created
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
 updated
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
2
 deleted
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
2
 invalidateCaches
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 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\Plans;
6use App\Http\Services\CacheInvalidationService;
7use Illuminate\Support\Facades\Log;
8
9/**
10 * Observer for Plan model changes.
11 *
12 * Handles cache invalidation when plans are created, updated, or deleted.
13 * Also dispatches jobs to invalidate user subscription caches for affected users.
14 */
15class PlanObserver
16{
17    /**
18     * Cache key prefix for plans.
19     */
20    private const CACHE_KEY_PREFIX = 'plans:';
21
22    /**
23     * Handle the Plans "created" event.
24     */
25    public function created(Plans $plan): void
26    {
27        $this->invalidateCaches($plan);
28
29        Log::info('Plan created', [
30            'plan_id' => (string) $plan->_id,
31            'identifier' => $plan->identifier,
32        ]);
33    }
34
35    /**
36     * Handle the Plans "updated" event.
37     */
38    public function updated(Plans $plan): void
39    {
40        Log::info('PlanObserver::updated called', [
41            'plan_id' => (string) $plan->_id,
42            'stripe_id' => $plan->stripe_id,
43        ]);
44
45        $this->invalidateCaches($plan);
46
47        // Dispatch job to invalidate user subscription caches for affected users
48        if ($plan->stripe_id) {
49            Log::info('PlanObserver: Dispatching user subscription cache invalidation job', [
50                'stripe_id' => $plan->stripe_id,
51            ]);
52            $this->getCacheInvalidationService()->invalidateUserSubscriptionCachesByPlan($plan->stripe_id);
53        }
54
55        Log::info('Plan updated', [
56            'plan_id' => (string) $plan->_id,
57            'identifier' => $plan->identifier,
58            'changes' => $plan->getChanges(),
59        ]);
60    }
61
62    /**
63     * Handle the Plans "deleted" event.
64     */
65    public function deleted(Plans $plan): void
66    {
67        $this->invalidateCaches($plan);
68
69        // Dispatch job to invalidate user subscription caches for affected users
70        if ($plan->stripe_id) {
71            $this->getCacheInvalidationService()->invalidateUserSubscriptionCachesByPlan($plan->stripe_id);
72        }
73
74        Log::info('Plan deleted', [
75            'plan_id' => (string) $plan->_id,
76            'identifier' => $plan->identifier,
77        ]);
78    }
79
80    /**
81     * Invalidate all caches related to this plan.
82     */
83    private function invalidateCaches(Plans $plan): void
84    {
85        $this->getCacheInvalidationService()->invalidatePlanCaches(
86            (string) $plan->_id,
87            $plan->identifier
88        );
89    }
90
91    /**
92     * Get the CacheInvalidationService instance.
93     */
94    private function getCacheInvalidationService(): CacheInvalidationService
95    {
96        return app(CacheInvalidationService::class);
97    }
98}