Test Failed
Pull Request — master (#83)
by Бабичев
04:54
created

Cart::alreadyBuy()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 4.0032

Importance

Changes 0
Metric Value
cc 4
eloc 16
c 0
b 0
f 0
nc 6
nop 2
dl 0
loc 27
ccs 16
cts 17
cp 0.9412
crap 4.0032
rs 9.7333
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
     */
29 5
    public static function make(): self
30
    {
31 5
        return new static();
32
    }
33
34
    /**
35
     * @param Product $product
36
     * @param int $quantity
37
     * @return static
38
     */
39 5
    public function addItem(Product $product, int $quantity = 1): self
40
    {
41 5
        $this->addQuantity($product, $quantity);
42 5
        for ($i = 0; $i < $quantity; $i++) {
43 5
            $this->items[] = $product;
44
        }
45 5
        return $this;
46
    }
47
48
    /**
49
     * @param iterable $products
50
     * @return static
51
     */
52 1
    public function addItems(iterable $products): self
53
    {
54 1
        foreach ($products as $product) {
55 1
            $this->addItem($product);
56
        }
57
58 1
        return $this;
59
    }
60
61
    /**
62
     * @return Product[]
63
     */
64 4
    public function getItems(): array
65
    {
66 4
        return $this->items;
67
    }
68
69
    /**
70
     * @return Product[]
71
     */
72 2
    public function getUniqueItems(): array
73
    {
74 2
        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 2
    public function alreadyBuy(Customer $customer, bool $gifts = null): array
85
    {
86 2
        $status = [Transfer::STATUS_PAID];
87 2
        if ($gifts) {
88
            $status[] = Transfer::STATUS_GIFT;
89
        }
90
91
        /**
92
         * @var Transfer $query
93
         */
94 2
        $result = [];
95 2
        $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 2
        foreach ($this->getUniqueItems() as $product) {
97 2
            $collect = (clone $query)
98 2
                ->where('to_type', $product->getMorphClass())
99 2
                ->where('to_id', $product->getKey())
100 2
                ->whereIn('status', $status)
101 2
                ->orderBy('id', 'desc')
102 2
                ->limit($this->getQuantity($product))
103 2
                ->get();
104
105 2
            foreach ($collect as $datum) {
106 2
                $result[] = $datum;
107
            }
108
        }
109
110 2
        return $result;
111
    }
112
113
    /**
114
     * @param Customer $customer
115
     * @param bool|null $force
116
     * @return bool
117
     */
118 3
    public function canBuy(Customer $customer, bool $force = null): bool
119
    {
120 3
        foreach ($this->items as $item) {
121 3
            if (!$item->canBuy($customer, $this->getQuantity($item), $force)) {
122 3
                return false;
123
            }
124
        }
125
126 3
        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
    public function count(): int
145
    {
146
        return count($this->items);
147
    }
148
149
    /**
150
     * @param Product $product
151
     * @return int
152
     */
153 5
    public function getQuantity(Product $product): int
154
    {
155 5
        $class = get_class($product);
156 5
        $uniq = $product->getUniqueId();
157 5
        return $this->quantity[$class][$uniq] ?? 0;
158
    }
159
160
    /**
161
     * @param Product $product
162
     * @param int $quantity
163
     */
164 5
    protected function addQuantity(Product $product, int $quantity): void
165
    {
166 5
        $class = get_class($product);
167 5
        $uniq = $product->getUniqueId();
168 5
        $this->quantity[$class][$uniq] = $this->getQuantity($product) + $quantity;
169 5
    }
170
171
}
172