Completed
Pull Request — master (#107)
by Бабичев
177:43 queued 73:46
created

Cart::make()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
1
<?php
2
3
namespace Bavix\Wallet\Objects;
4
5
use Bavix\Wallet\Interfaces\Customer;
6
use Bavix\Wallet\Interfaces\Product;
7
use Bavix\Wallet\Models\Transfer;
8
use Countable;
9
use function array_unique;
10
use function count;
11
use function get_class;
12
13
class Cart implements Countable
14
{
15
16
    /**
17
     * @var Product[]
18
     */
19
    protected $items = [];
20
21
    /**
22
     * @var int[]
23
     */
24
    protected $quantity = [];
25
26
    /**
27
     * @return static
28
     * @deprecated use app(MakeService::class)->makeCart()
29
     */
30
    public static function make(): self
31
    {
32
        return new static();
33
    }
34
35
    /**
36
     * @param Product $product
37
     * @param int $quantity
38
     * @return static
39
     */
40 20
    public function addItem(Product $product, int $quantity = 1): self
41
    {
42 20
        $this->addQuantity($product, $quantity);
43 20
        for ($i = 0; $i < $quantity; $i++) {
44 20
            $this->items[] = $product;
45
        }
46 20
        return $this;
47
    }
48
49
    /**
50
     * @param iterable $products
51
     * @return static
52
     */
53 4
    public function addItems(iterable $products): self
54
    {
55 4
        foreach ($products as $product) {
56 4
            $this->addItem($product);
57
        }
58
59 4
        return $this;
60
    }
61
62
    /**
63
     * @return Product[]
64
     */
65 18
    public function getItems(): array
66
    {
67 18
        return $this->items;
68
    }
69
70
    /**
71
     * @return Product[]
72
     */
73 19
    public function getUniqueItems(): array
74
    {
75 19
        return array_unique($this->items);
76
    }
77
78
    /**
79
     * The method returns the transfers already paid for the goods
80
     *
81
     * @param Customer $customer
82
     * @param bool|null $gifts
83
     * @return Transfer[]
84
     */
85 19
    public function alreadyBuy(Customer $customer, bool $gifts = null): array
86
    {
87 19
        $status = [Transfer::STATUS_PAID];
88 19
        if ($gifts) {
89 3
            $status[] = Transfer::STATUS_GIFT;
90
        }
91
92
        /**
93
         * @var Transfer $query
94
         */
95 19
        $result = [];
96 19
        $query = $customer->transfers();
0 ignored issues
show
Bug introduced by
The method transfers() does not exist on Bavix\Wallet\Interfaces\Customer. Did you maybe mean transfer()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

96
        /** @scrutinizer ignore-call */ 
97
        $query = $customer->transfers();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
97 19
        foreach ($this->getUniqueItems() as $product) {
98 19
            $collect = (clone $query)
99 19
                ->where('to_type', $product->getMorphClass())
100 19
                ->where('to_id', $product->getKey())
101 19
                ->whereIn('status', $status)
102 19
                ->orderBy('id', 'desc')
103 19
                ->limit($this->getQuantity($product))
104 19
                ->get();
105
106 19
            foreach ($collect as $datum) {
107 19
                $result[] = $datum;
108
            }
109
        }
110
111 19
        return $result;
112
    }
113
114
    /**
115
     * @param Customer $customer
116
     * @param bool|null $force
117
     * @return bool
118
     */
119 17
    public function canBuy(Customer $customer, bool $force = null): bool
120
    {
121 17
        foreach ($this->items as $item) {
122 17
            if (!$item->canBuy($customer, $this->getQuantity($item), $force)) {
123 17
                return false;
124
            }
125
        }
126
127 17
        return true;
128
    }
129
130
    /**
131
     * @return int
132
     */
133 2
    public function getTotal(): int
134
    {
135 2
        $result = 0;
136 2
        foreach ($this->items as $item) {
137 2
            $result += $item->getAmountProduct();
138
        }
139 2
        return $result;
140
    }
141
142
    /**
143
     * @return int
144
     */
145 14
    public function count(): int
146
    {
147 14
        return count($this->items);
148
    }
149
150
    /**
151
     * @param Product $product
152
     * @return int
153
     */
154 20
    public function getQuantity(Product $product): int
155
    {
156 20
        $class = get_class($product);
157 20
        $uniq = $product->getUniqueId();
158 20
        return $this->quantity[$class][$uniq] ?? 0;
159
    }
160
161
    /**
162
     * @param Product $product
163
     * @param int $quantity
164
     */
165 20
    protected function addQuantity(Product $product, int $quantity): void
166
    {
167 20
        $class = get_class($product);
168 20
        $uniq = $product->getUniqueId();
169 20
        $this->quantity[$class][$uniq] = $this->getQuantity($product) + $quantity;
170 20
    }
171
172
}
173