Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
75.00% covered (warning)
75.00%
9 / 12
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
FamilyUpdateService
75.00% covered (warning)
75.00%
9 / 12
0.00% covered (danger)
0.00%
0 / 1
3.14
0.00% covered (danger)
0.00%
0 / 1
 update
75.00% covered (warning)
75.00%
9 / 12
0.00% covered (danger)
0.00%
0 / 1
3.14
1<?php
2
3namespace Domain\Category\Services;
4
5use App\Events\ServiceProcessed;
6use App\Models\Category\Family;
7use App\Models\User;
8use Domain\Category\DTOs\FamilyDTO;
9use Domain\Integration\Services\IntegrationUpdateService;
10use Illuminate\Support\Facades\DB;
11use Throwable;
12
13class 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}