Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
71.43% |
10 / 14 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| RemoveUserExpoTokenService | |
71.43% |
10 / 14 |
|
50.00% |
1 / 2 |
5.58 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| remove | |
63.64% |
7 / 11 |
|
0.00% |
0 / 1 |
4.77 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Domain\User\Services; |
| 4 | |
| 5 | use App\Events\ServiceProcessed; |
| 6 | use App\Models\User; |
| 7 | |
| 8 | readonly class RemoveUserExpoTokenService |
| 9 | { |
| 10 | private ?string $expoToken; |
| 11 | private User $userToUpdate; |
| 12 | private User $user; |
| 13 | |
| 14 | /** |
| 15 | * @param User $userToUpdate |
| 16 | * @param User $user |
| 17 | * @param string|null $expoToken |
| 18 | */ |
| 19 | public function __construct(User $userToUpdate, User $user, ?string $expoToken = null) |
| 20 | { |
| 21 | $this->expoToken = $expoToken; |
| 22 | $this->userToUpdate = $userToUpdate; |
| 23 | $this->user = $user; |
| 24 | } |
| 25 | |
| 26 | public function remove(): User |
| 27 | { |
| 28 | $tokens = $this->user->expo_token ?? []; |
| 29 | |
| 30 | if (!is_array($tokens)) { |
| 31 | $tokens = json_decode($tokens, true) ?? []; |
| 32 | } |
| 33 | |
| 34 | if ($this->expoToken) { |
| 35 | $tokens = array_values( |
| 36 | array_filter($tokens, fn($t) => $t !== $this->expoToken) |
| 37 | ); |
| 38 | } |
| 39 | |
| 40 | $this->userToUpdate->expo_token = empty($tokens) ? null : $tokens; |
| 41 | $this->userToUpdate->save(); |
| 42 | |
| 43 | ServiceProcessed::dispatch(self::class, $this->user); |
| 44 | |
| 45 | return $this->userToUpdate; |
| 46 | } |
| 47 | } |