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