Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 16 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| UpdateUserExpoTokenService | |
0.00% |
0 / 16 |
|
0.00% |
0 / 3 |
30 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| update | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 | |||
| processExistsUserWithExpoToken | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Domain\User\Services; |
| 4 | |
| 5 | use App\Events\ServiceProcessed; |
| 6 | use App\Models\User; |
| 7 | use Domain\User\DTOs\UserDTO; |
| 8 | |
| 9 | readonly class UpdateUserExpoTokenService |
| 10 | { |
| 11 | private string $expoToken; |
| 12 | private User $userToUpdate; |
| 13 | private User $user; |
| 14 | |
| 15 | /** |
| 16 | * @param string $expoToken |
| 17 | * @param User $userToUpdate |
| 18 | * @param User $user |
| 19 | */ |
| 20 | public function __construct(string $expoToken, User $userToUpdate, User $user) |
| 21 | { |
| 22 | $this->expoToken = $expoToken; |
| 23 | $this->userToUpdate = $userToUpdate; |
| 24 | $this->user = $user; |
| 25 | } |
| 26 | |
| 27 | |
| 28 | public function update(): User |
| 29 | { |
| 30 | $this->processExistsUserWithExpoToken(); |
| 31 | |
| 32 | $tokens = $this->userToUpdate->expo_token ?? []; |
| 33 | |
| 34 | if (!in_array($this->expoToken, $tokens, true)) { |
| 35 | $tokens[] = $this->expoToken; |
| 36 | } |
| 37 | |
| 38 | $this->userToUpdate->expo_token = $tokens; |
| 39 | $this->userToUpdate->save(); |
| 40 | |
| 41 | ServiceProcessed::dispatch(self::class, $this->user); |
| 42 | return $this->userToUpdate; |
| 43 | } |
| 44 | |
| 45 | private function processExistsUserWithExpoToken() |
| 46 | { |
| 47 | $users = User::query() |
| 48 | ->whereJsonContains('expo_token', $this->expoToken) |
| 49 | ->get(); |
| 50 | |
| 51 | foreach ($users as $user) { |
| 52 | (new RemoveUserExpoTokenService($user, $user, $this->expoToken))->remove(); |
| 53 | } |
| 54 | } |
| 55 | } |