Completed
Pull Request — master (#45)
by Бабичев
06:01
created

Cart::getQuantity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 count;
10
use function get_class;
11
use function array_unique;
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
     */
29 14
    public static function make(): self
30
    {
31 14
        return new static();
32
    }
33
34
    /**
35
     * @param Product $product
36
     * @param int $quantity
37
     * @return static
38
     */
39 14
    public function addItem(Product $product, int $quantity = 1): self
40
    {
41 14
        $this->addQuantity($product, $quantity);
42 14
        for ($i = 0; $i < $quantity; $i++) {
43 14
            $this->items[] = $product;
44
        }
45 14
        return $this;
46
    }
47
48
    /**
49
     * @param iterable $products
50
     * @return static
51
     */
52 2
    public function addItems(iterable $products): self
53
    {
54 2
        foreach ($products as $product) {
55 2
            $this->addItem($product);
56
        }
57
58 2
        return $this;
59
    }
60
61
    /**
62
     * @return Product[]
63
     */
64 13
    public function getItems(): array
65
    {
66 13
        return $this->items;
67
    }
68
69
    /**
70
     * @return Product[]
71
     */
72 13
    public function getUniqueItems(): array
73
    {
74 13
        return array_unique($this->items);
75
    }
76
77
    /**
78
     * The method returns the transfers already paid for the goods
79
     *
80
     * @param Customer $customer
81
     * @param bool|null $gifts
82
     * @return Transfer[]
83
     */
84 13
    public function alreadyBuy(Customer $customer, bool $gifts = null): array
85
    {
86 13
        $status = [Transfer::STATUS_PAID];
87 13
        if ($gifts) {
88 2
            $status[] = Transfer::STATUS_GIFT;
89
        }
90
91
        /**
92
         * @var Transfer $query
93
         */
94 13
        $result = [];
95 13
        $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

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