Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
SubscriptionObserver
0.00% covered (danger)
0.00%
0 / 36
0.00% covered (danger)
0.00%
0 / 6
210
0.00% covered (danger)
0.00%
0 / 1
 created
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 updated
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 deleted
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 invalidateSubscriptionCache
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 canSync
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
12
 checkUserPersonas
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
30
1<?php
2
3namespace App\Observers;
4
5use App\Http\Models\Auth\User;
6use App\Http\Models\Subscription;
7use App\Http\Models\UserPersona;
8use App\Http\Services\CurrentSubscriptionService;
9use App\Jobs\ProcessSubscriptionAsyncJob;
10use App\Traits\SubscriptionTrait;
11
12class SubscriptionObserver
13{
14    use SubscriptionTrait;
15
16    public function created(Subscription $subscription): void
17    {
18        $this->invalidateSubscriptionCache($subscription);
19
20        if ($this->canSync($subscription->user_id)) {
21            ProcessSubscriptionAsyncJob::dispatch($subscription);
22        }
23
24        $this->checkUserPersonas($subscription);
25    }
26
27    public function updated(Subscription $subscription): void
28    {
29        $this->invalidateSubscriptionCache($subscription);
30
31        if ($this->canSync($subscription->user_id)) {
32            ProcessSubscriptionAsyncJob::dispatch($subscription);
33        }
34
35        $this->checkUserPersonas($subscription);
36    }
37
38    public function deleted(Subscription $subscription): void
39    {
40        $this->invalidateSubscriptionCache($subscription);
41    }
42
43    private function invalidateSubscriptionCache(Subscription $subscription): void
44    {
45        app(CurrentSubscriptionService::class)->invalidateCache($subscription->user_id);
46    }
47
48    private function canSync(string $userId)
49    {
50        $user = User::find($userId);
51
52        if (! $user) {
53            return false;
54        }
55
56        return $user->status !== 'Deactivated' && empty($user->deleted_at);
57    }
58
59    private function checkUserPersonas(Subscription $subscription)
60    {
61        $user = User::find($subscription->user_id);
62
63        if (! $user) {
64            return;
65        }
66
67        $personaCount = UserPersona::withoutGlobalScopes()->where('user_id', $user->_id)->count();
68        $plan = $this->getCurrentPlan($user);
69
70        if ($plan->user_persona_available >= $personaCount) {
71            UserPersona::withoutGlobalScopes()->where('user_id', $user->_id)
72                ->update(['disabled' => false]);
73
74            return;
75        }
76
77        $defaultUserPersona = UserPersona::withoutGlobalScopes()->where('user_id', $user->_id)
78            ->where('is_default', true)
79            ->first();
80
81        $validUserPersonas = UserPersona::withoutGlobalScopes()->where('user_id', $user->_id)
82            ->orderBy('created_at', 'asc')
83            ->limit($plan->user_persona_available)
84            ->get();
85
86        if ($defaultUserPersona && ! $validUserPersonas->contains($defaultUserPersona)) {
87            $validUserPersonas->pop();
88            $validUserPersonas->push($defaultUserPersona);
89        }
90
91        UserPersona::withoutGlobalScopes()->where('user_id', $user->_id)
92            ->whereNotIn('_id', $validUserPersonas->pluck('_id'))
93            ->update(['disabled' => true]);
94    }
95}