Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| FamilyPriorityConfigurationService | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
3 | |
100.00% |
1 / 1 |
| update | |
100.00% |
13 / 13 |
|
100.00% |
1 / 1 |
3 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Domain\Category\Services; |
| 4 | |
| 5 | use App\Models\User; |
| 6 | use App\Models\Address\Address; |
| 7 | use App\Models\Product\Product; |
| 8 | use App\Models\Category\Family; |
| 9 | use App\Models\Item\Item; |
| 10 | use App\Events\ServiceProcessed; |
| 11 | |
| 12 | use Domain\Product\DTOs\ProductPriorityConfigurationDTO; |
| 13 | use Domain\Item\Exceptions\ItemRelationExistsException; |
| 14 | |
| 15 | readonly 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 | } |