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