Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
72.22% |
13 / 18 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| AddressStoreService | |
72.22% |
13 / 18 |
|
0.00% |
0 / 1 |
4.34 | |
0.00% |
0 / 1 |
| store | |
72.22% |
13 / 18 |
|
0.00% |
0 / 1 |
4.34 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Domain\Address\Services; |
| 4 | |
| 5 | use App\Events\ServiceProcessed; |
| 6 | use App\Models\Address\Address; |
| 7 | use App\Models\User; |
| 8 | use Domain\Address\DTOs\AddressDTO; |
| 9 | use Domain\Transferable\Services\TransferableStoreService; |
| 10 | use Exception; |
| 11 | use Illuminate\Support\Facades\DB; |
| 12 | |
| 13 | readonly class AddressStoreService |
| 14 | { |
| 15 | |
| 16 | /** |
| 17 | * @param AddressDTO $addressDTO |
| 18 | * @param User $user |
| 19 | * @return Address |
| 20 | * @throws Exception |
| 21 | */ |
| 22 | public function store(AddressDTO $addressDTO, User $user): Address |
| 23 | { |
| 24 | try { |
| 25 | DB::beginTransaction(); |
| 26 | |
| 27 | $address = new Address(); |
| 28 | $address->fill($addressDTO->toArray()); |
| 29 | $address->parent()->associate($addressDTO->parent); |
| 30 | |
| 31 | if ($addressDTO->parent) { |
| 32 | $address->setAttribute('sector', $address->firstParent()->getAttribute('sector')->value); |
| 33 | } else { |
| 34 | if($user->isMaster()){ |
| 35 | $address->setAttribute('sector', 0); |
| 36 | } else { |
| 37 | $address->setAttribute('sector', $user->currentSector()); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | (new AddressUpdateOrderService)->updateOrder($address, $user); |
| 42 | $address->save(); |
| 43 | |
| 44 | (new TransferableStoreService())->store($address); |
| 45 | |
| 46 | DB::commit(); |
| 47 | } catch (Exception $exception) { |
| 48 | DB::rollBack(); |
| 49 | throw $exception; |
| 50 | } |
| 51 | |
| 52 | ServiceProcessed::dispatch(self::class, $user); |
| 53 | return $address; |
| 54 | } |
| 55 | } |