Passed
Push — master ( 1c59b9...10afab )
by Бабичев
568:30 queued 480:56
created

Cart   A

Complexity

Total Complexity 19

Size/Duplication

Total Lines 158
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 4
Bugs 2 Features 2
Metric Value
eloc 43
c 4
b 2
f 2
dl 0
loc 158
ccs 50
cts 50
cp 1
rs 10
wmc 19

11 Methods

Rating   Name   Duplication   Size   Complexity  
A make() 0 3 1
A getTotal() 0 7 2
A addItem() 0 7 2
A addItems() 0 7 2
A canBuy() 0 9 3
A addQuantity() 0 5 1
A getItems() 0 3 1
A count() 0 3 1
A getQuantity() 0 5 1
A getUniqueItems() 0 3 1
A alreadyBuy() 0 27 4
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(Cart::class)
29
     * @codeCoverageIgnore
30
     */
31
    public static function make(): self
32
    {
33
        return new static();
34
    }
35
36
    /**
37
     * @param Product $product
38
     * @param int $quantity
39
     * @return static
40
     */
41 20
    public function addItem(Product $product, int $quantity = 1): self
42
    {
43 20
        $this->addQuantity($product, $quantity);
44 20
        for ($i = 0; $i < $quantity; $i++) {
45 20
            $this->items[] = $product;
46
        }
47 20
        return $this;
48
    }
49
50
    /**
51
     * @param iterable $products
52
     * @return static
53
     */
54 4
    public function addItems(iterable $products): self
55
    {
56 4
        foreach ($products as $product) {
57 4
            $this->addItem($product);
58
        }
59
60 4
        return $this;
61
    }
62
63
    /**
64
     * @return Product[]
65
     */
66 18
    public function getItems(): array
67
    {
68 18
        return $this->items;
69
    }
70
71
    /**
72
     * @return Product[]
73
     */
74 19
    public function getUniqueItems(): array
75
    {
76 19
        return array_unique($this->items);
77
    }
78
79
    /**
80
     * The method returns the transfers already paid for the goods
81
     *
82
     * @param Customer $customer
83
     * @param bool|null $gifts
84
     * @return Transfer[]
85
     */
86 19
    public function alreadyBuy(Customer $customer, bool $gifts = null): array
87
    {
88 19
        $status = [Transfer::STATUS_PAID];
89 19
        if ($gifts) {
90 3
            $status[] = Transfer::STATUS_GIFT;
91
        }
92
93
        /**
94
         * @var Transfer $query
95
         */
96 19
        $result = [];
97 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

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