Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 46 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
UserObserver | |
0.00% |
0 / 46 |
|
0.00% |
0 / 8 |
812 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
created | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
20 | |||
updated | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
20 | |||
deleted | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 | |||
restored | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
12 | |||
forceDeleted | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
performAction | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
56 | |||
canSync | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | |
3 | namespace App\Observers; |
4 | |
5 | use App\Helpers\FlyMSGLogger; |
6 | use App\Http\Models\Auth\User; |
7 | use App\Http\Models\Admin\CompanyLicenses; |
8 | use App\Jobs\ProcessUserAsyncJob; |
9 | use App\Services\Email\EmailService; |
10 | |
11 | class UserObserver |
12 | { |
13 | public function __construct(private readonly EmailService $emailService) {} |
14 | |
15 | /** |
16 | * Handle the User "created" event. |
17 | */ |
18 | public function created(User $user): void |
19 | { |
20 | if ($this->canSync($user->id)) { |
21 | ProcessUserAsyncJob::dispatch($user, 'created', $this->emailService)->onQueue('high'); |
22 | } |
23 | |
24 | if ($authUser = auth()->user()) { |
25 | $authUserModel = User::find($authUser->id); |
26 | if ($authUserModel) { |
27 | $this->performAction($authUserModel, $user, 'added', 'yes_community'); |
28 | } |
29 | } |
30 | } |
31 | |
32 | /** |
33 | * Handle the User "updated" event. |
34 | */ |
35 | public function updated(User $user): void |
36 | { |
37 | if ($this->canSync($user->id)) { |
38 | $dirty = $user->getDirty(); |
39 | if (count($dirty) === 1 && array_key_exists('user_info_id', $dirty)) { |
40 | return; |
41 | } |
42 | ProcessUserAsyncJob::dispatch($user, 'updated', $this->emailService); |
43 | } |
44 | } |
45 | |
46 | /** |
47 | * Handle the User "deleted" event. |
48 | */ |
49 | public function deleted(User $user): void |
50 | { |
51 | $user->update([ |
52 | 'status' => "Deactivated", |
53 | 'deactivated_at' => now() |
54 | ]); |
55 | |
56 | if ($authUser = auth()->user()) { |
57 | $authUserModel = User::find($authUser->id); |
58 | if ($authUserModel) { |
59 | $this->performAction($authUserModel, $user, 'deactivated'); |
60 | } |
61 | } |
62 | } |
63 | |
64 | /** |
65 | * Handle the User "restored" event. |
66 | */ |
67 | public function restored(User $user): void |
68 | { |
69 | $user->update([ |
70 | 'status' => "Active", |
71 | 'activation_date' => now(), |
72 | ]); |
73 | |
74 | $user->unset('deleted_at'); |
75 | $user->unset('deactivated_at'); |
76 | $user->save(); |
77 | |
78 | if ($authUser = auth()->user()) { |
79 | $authUserModel = User::find($authUser->id); |
80 | if ($authUserModel) { |
81 | $this->performAction($authUserModel, $user, 'reactivated'); |
82 | } |
83 | } |
84 | } |
85 | |
86 | /** |
87 | * Handle the User "force deleted" event. |
88 | */ |
89 | public function forceDeleted(User $user): void |
90 | { |
91 | if ($authUser = auth()->user()) { |
92 | $authUserModel = User::find($authUser->id); |
93 | if ($authUserModel) { |
94 | $this->performAction($authUserModel, $user, 'deleted'); |
95 | } |
96 | } |
97 | } |
98 | |
99 | private function performAction(User $authUser, User $user, string $action, string $businessProChoice = 'yes_dedicated') |
100 | { |
101 | if ($authUser->isAdmin() && filled($user->company)) { |
102 | try { |
103 | $companyLicense = CompanyLicenses::where('company_id', $user->company_id)->active()->first(); |
104 | |
105 | if ( |
106 | $companyLicense && |
107 | $companyLicense->business_pro_enterprise_plus && |
108 | in_array($businessProChoice, $companyLicense->business_pro_enterprise_plus) |
109 | ) { |
110 | } |
111 | } catch (\Exception $e) { |
112 | FlyMSGLogger::logError(__METHOD__, $e); |
113 | } |
114 | } |
115 | } |
116 | |
117 | private function canSync(string $userId) |
118 | { |
119 | $user = User::find($userId); |
120 | |
121 | if (!$user) { |
122 | return false; |
123 | } |
124 | |
125 | return $user->status !== 'Deactivated' && empty($user->deleted_at); |
126 | } |
127 | } |