Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
93.75% |
15 / 16 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| ProductLocationService | |
93.75% |
15 / 16 |
|
66.67% |
2 / 3 |
3.00 | |
0.00% |
0 / 1 |
| addresses | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| addressesForSector | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| query | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Domain\Product\Services; |
| 4 | |
| 5 | use App\Models\Address\Address; |
| 6 | use App\Models\Product\Product; |
| 7 | use App\Models\User; |
| 8 | use Illuminate\Database\Eloquent\Builder; |
| 9 | use Illuminate\Database\Eloquent\Collection; |
| 10 | use Illuminate\Database\Eloquent\Relations\MorphMany; |
| 11 | |
| 12 | readonly class ProductLocationService |
| 13 | { |
| 14 | |
| 15 | /** |
| 16 | * @param Product $product |
| 17 | * @return Collection |
| 18 | */ |
| 19 | public function addresses(Product $product): Collection |
| 20 | { |
| 21 | return $this->query($product)->get(); |
| 22 | } |
| 23 | |
| 24 | public function addressesForSector(Product $product, User $user): Collection |
| 25 | { |
| 26 | return $this->query($product) |
| 27 | ->forSector($user) |
| 28 | ->get(); |
| 29 | } |
| 30 | |
| 31 | private function query(Product $product): Builder |
| 32 | { |
| 33 | return Address::query() |
| 34 | ->with(['parent', 'transferable', 'items' => function (MorphMany $query) use ($product) { |
| 35 | $query->where('items.product_id', $product->getKey()); |
| 36 | }]) |
| 37 | ->withSum(['items' => function (Builder $query) use ($product) { |
| 38 | $query->where('items.product_id', $product->getKey()); |
| 39 | }], 'amount') |
| 40 | ->whereHas('items', function (Builder $query) use ($product) { |
| 41 | $query->whereHas('product', function (Builder $query) use ($product) { |
| 42 | $query->where('id', $product->getKey()); |
| 43 | }); |
| 44 | }); |
| 45 | } |
| 46 | } |