Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
75.68% covered (warning)
75.68%
28 / 37
25.00% covered (danger)
25.00%
1 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
CheckItemCanTransferService
75.68% covered (warning)
75.68%
28 / 37
25.00% covered (danger)
25.00%
1 / 4
16.82
0.00% covered (danger)
0.00%
0 / 1
 check
90.00% covered (success)
90.00%
9 / 10
0.00% covered (danger)
0.00%
0 / 1
5.03
 validateProduct
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
3
 validateFamily
55.56% covered (warning)
55.56%
5 / 9
0.00% covered (danger)
0.00%
0 / 1
3.79
 validateOrigin
55.56% covered (warning)
55.56%
5 / 9
0.00% covered (danger)
0.00%
0 / 1
3.79
1<?php
2
3namespace Domain\Transferable\Services;
4
5use App\Models\Address\Address;
6use App\Models\Category\Family;
7use App\Models\Category\Origin;
8use App\Models\Item\Item;
9use App\Models\Product\Product;
10use Domain\Item\Interfaces\HasItemsInterface;
11use Domain\Transferable\Exceptions\CantTransferItemException;
12use Domain\Transferable\Exceptions\ItemOwnershipException;
13use function __;
14
15readonly class CheckItemCanTransferService
16{
17    /**
18     * @param HasItemsInterface $sender
19     * @param HasItemsInterface $recipient
20     * @param Item $item
21     * @return void
22     */
23    public function check(HasItemsInterface $sender, HasItemsInterface $recipient, Item $item): void
24    {
25        if (!$sender->items()->where('id', $item->getKey())->exists()) {
26            throw new ItemOwnershipException(__("Item does not belong to the sender"));
27        }
28
29        if (!$item->hasAmount()) {
30            throw new CantTransferItemException(__("Item no quantity.Contact a system administrator."));
31        }
32
33        if ($recipient instanceof Address) {
34            $product = $item->product;
35
36            if ($recipient->addressPermissions()->exists()) {
37
38                $this->validateProduct($recipient, $product);
39                $this->validateFamily($recipient, $product->family, $product);
40                $this->validateOrigin($recipient, $product->family->origin, $product);
41
42            }
43        }
44    }
45
46    private function validateProduct(Address $recipient, Product $product): void
47    {
48        $productList = $recipient->addressPermissions()
49            ->whereNotNull('product_id')
50            ->get()
51            ->pluck('product_id');
52
53        if ($productList->isNotEmpty() && $productList->doesntContain($product->getKey())) {
54            throw new CantTransferItemException(__("The product :product does not belong to the list of products released for the address :address.", [
55                'product' => $product->getKey(),
56                'address' => $recipient->getKey()
57            ]));
58        }
59    }
60
61    private function validateFamily(Address $recipient, Family $family, Product $product): void
62    {
63        $familyList = $recipient->addressPermissions()
64            ->whereNotNull('family_id')
65            ->get()
66            ->pluck('family_id');
67
68
69        if ($familyList->isNotEmpty() && $familyList->doesntContain($family->getKey())) {
70            throw new CantTransferItemException(__("The product :product cannot be stored at :address due to lack of permission, the product does not belong to the family.", [
71                'product' => $product->getKey(),
72                'address' => $recipient->getKey(),
73            ]));
74        }
75    }
76
77    private function validateOrigin(Address $recipient, Origin $origin, Product $product): void
78    {
79        $originList = $recipient->addressPermissions()
80            ->whereNotNull('origin_id')
81            ->get()
82            ->pluck('origin_id');
83
84        if ($originList->isNotEmpty() && $originList->doesntContain($origin->getKey())) {
85            throw new CantTransferItemException(__("The product :product cannot be stored at :address due to lack of permission, the product does not belong to the origin.", [
86                'product' => $product->getKey(),
87                'address' => $recipient->getKey(),
88            ]));
89        }
90    }
91}