Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| TeamUpdateService | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
20 | |
0.00% |
0 / 1 |
| update | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
20 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Domain\Team\Services; |
| 4 | |
| 5 | use App\Events\ServiceProcessed; |
| 6 | use App\Models\Team\Team; |
| 7 | use App\Models\User; |
| 8 | use Domain\Shared\Enums\SectorEnum; |
| 9 | use Domain\Team\DTOs\TeamDTO; |
| 10 | use Illuminate\Support\Facades\DB; |
| 11 | use Throwable; |
| 12 | use function array_values; |
| 13 | |
| 14 | readonly class TeamUpdateService |
| 15 | { |
| 16 | /** |
| 17 | * @param TeamDTO $teamDTO |
| 18 | * @param Team $team |
| 19 | * @param User $user |
| 20 | * @return Team |
| 21 | * @throws Throwable |
| 22 | */ |
| 23 | public function update(TeamDTO $teamDTO, Team $team, User $user): Team |
| 24 | { |
| 25 | /** @var SectorEnum $sector */ |
| 26 | $sector = SectorEnum::tryFrom($user->currentSector()); |
| 27 | (new CheckUserSectorService())->check($sector, $teamDTO->users); |
| 28 | |
| 29 | try { |
| 30 | DB::beginTransaction(); |
| 31 | |
| 32 | if ($teamDTO->default) { |
| 33 | Team::query() |
| 34 | ->where('sector', $sector->value) |
| 35 | ->where('id', '!=', $team->getKey()) |
| 36 | ->update(['default' => false]); |
| 37 | } |
| 38 | |
| 39 | $team->fill($teamDTO->toArray()); |
| 40 | if ($team->isDirty()) { |
| 41 | $team->save(); |
| 42 | } |
| 43 | |
| 44 | $team->users()->sync($teamDTO->users); |
| 45 | |
| 46 | DB::commit(); |
| 47 | } catch (Throwable $exception) { |
| 48 | DB::rollBack(); |
| 49 | throw $exception; |
| 50 | } |
| 51 | |
| 52 | ServiceProcessed::dispatch(self::class, $user); |
| 53 | return $team; |
| 54 | } |
| 55 | } |