Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
75.00% |
9 / 12 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| FamilyUpdateService | |
75.00% |
9 / 12 |
|
0.00% |
0 / 1 |
3.14 | |
0.00% |
0 / 1 |
| update | |
75.00% |
9 / 12 |
|
0.00% |
0 / 1 |
3.14 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Domain\Category\Services; |
| 4 | |
| 5 | use App\Events\ServiceProcessed; |
| 6 | use App\Models\Category\Family; |
| 7 | use App\Models\User; |
| 8 | use Domain\Category\DTOs\FamilyDTO; |
| 9 | use Domain\Integration\Services\IntegrationUpdateService; |
| 10 | use Illuminate\Support\Facades\DB; |
| 11 | use Throwable; |
| 12 | |
| 13 | class FamilyUpdateService |
| 14 | { |
| 15 | |
| 16 | /** |
| 17 | * @param FamilyDTO $familyDTO |
| 18 | * @param Family $family |
| 19 | * @param User $user |
| 20 | * @return Family |
| 21 | * @throws Throwable |
| 22 | */ |
| 23 | public function update(FamilyDTO $familyDTO, Family $family, User $user): Family |
| 24 | { |
| 25 | |
| 26 | try { |
| 27 | DB::beginTransaction(); |
| 28 | |
| 29 | $family->fill($familyDTO->toArray()); |
| 30 | $family->origin()->associate($familyDTO->origin); |
| 31 | if ($family->isDirty()) { |
| 32 | $family->save(); |
| 33 | } |
| 34 | |
| 35 | (new IntegrationUpdateService())->update($familyDTO->integrationFamilyDTO, $family->integration, $user); |
| 36 | |
| 37 | DB::commit(); |
| 38 | } catch (Throwable $exception) { |
| 39 | DB::rollBack(); |
| 40 | throw $exception; |
| 41 | } |
| 42 | |
| 43 | ServiceProcessed::dispatch(self::class, $user); |
| 44 | return $family; |
| 45 | } |
| 46 | } |