Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
78.57% |
11 / 14 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
| PickedItemStoreService | |
78.57% |
11 / 14 |
|
0.00% |
0 / 1 |
3.09 | |
0.00% |
0 / 1 |
| store | |
78.57% |
11 / 14 |
|
0.00% |
0 / 1 |
3.09 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Domain\PickingOrder\Services; |
| 4 | |
| 5 | use App\Events\ServiceProcessed; |
| 6 | use App\Models\PickedItem; |
| 7 | use App\Models\User; |
| 8 | use Domain\PickingOrder\DTOs\PickedItemDTO; |
| 9 | use Exception; |
| 10 | use Illuminate\Support\Facades\DB; |
| 11 | use Throwable; |
| 12 | |
| 13 | readonly class PickedItemStoreService |
| 14 | { |
| 15 | |
| 16 | /** |
| 17 | * @param PickedItemDTO $pickedItemDTO |
| 18 | * @param User $user |
| 19 | * @return PickedItem |
| 20 | * @throws Exception|Throwable |
| 21 | */ |
| 22 | public function store(PickedItemDTO $pickedItemDTO, User $user): PickedItem |
| 23 | { |
| 24 | try { |
| 25 | DB::beginTransaction(); |
| 26 | $pickedItem = new PickedItem(); |
| 27 | $pickedItem->fill($pickedItemDTO->toArray()); |
| 28 | $pickedItem->item()->associate($pickedItemDTO->item); |
| 29 | $pickedItem->stockIOProduct()->associate($pickedItemDTO->stockIOProduct); |
| 30 | |
| 31 | if (!is_null($pickedItemDTO->stockIOProduct)) { |
| 32 | $pickedItem->pickingOrderProduct()->associate($pickedItemDTO->pickingOrderProduct); |
| 33 | } |
| 34 | |
| 35 | $pickedItem->save(); |
| 36 | |
| 37 | DB::commit(); |
| 38 | } catch (Exception $exception) { |
| 39 | DB::rollBack(); |
| 40 | throw $exception; |
| 41 | } |
| 42 | |
| 43 | ServiceProcessed::dispatch(self::class, $user); |
| 44 | return $pickedItem; |
| 45 | } |
| 46 | } |