Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
FamilyPriorityConfigurationService
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
1 / 1
 update
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3namespace Domain\Category\Services;
4
5use App\Models\User;
6use App\Models\Address\Address;
7use App\Models\Product\Product;
8use App\Models\Category\Family;
9use App\Models\Item\Item;
10use App\Events\ServiceProcessed;
11
12use Domain\Product\DTOs\ProductPriorityConfigurationDTO;
13use Domain\Item\Exceptions\ItemRelationExistsException;
14
15readonly class FamilyPriorityConfigurationService
16{
17    public function update(
18      ProductPriorityConfigurationDTO $productPriorityConfigurationDTO,
19      Family $family,
20      User $user
21    ): Family {
22
23        $familyProducts = Product::where('family_id', $family->id)->pluck('id');
24
25        $count = Item::whereIn('product_id', $familyProducts)
26            ->where('itemable_type', Address::class)
27            ->count();
28
29        if($count > 0) {
30            throw new ItemRelationExistsException(
31                __('Some products already have an existing output configuration.')
32            );
33        }
34
35        $family->fill($productPriorityConfigurationDTO->toArray());
36
37        if ($family->isDirty()) {
38            $family->save();
39        }
40
41        ServiceProcessed::dispatch(self::class, $user);
42
43        return $family;
44    }
45}