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