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