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