Passed
Push — master ( 16f2ad...0b7e9e )
by Бабичев
01:40 queued 12s
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 0
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\Mathable;
7
use Bavix\Wallet\Interfaces\Product;
8
use Bavix\Wallet\Models\Transfer;
9
use Countable;
10
use function array_unique;
11
use function count;
12
use function get_class;
13
14
class Cart implements Countable
15
{
16
17
    /**
18
     * @var Product[]
19
     */
20
    protected $items = [];
21
22
    /**
23
     * @var int[]
24
     */
25
    protected $quantity = [];
26
27
    /**
28
     * @param Product $product
29
     * @param int $quantity
30
     * @return static
31
     */
32 41
    public function addItem(Product $product, int $quantity = 1): self
33
    {
34 41
        $this->addQuantity($product, $quantity);
35 41
        for ($i = 0; $i < $quantity; $i++) {
36 41
            $this->items[] = $product;
37
        }
38 41
        return $this;
39
    }
40
41
    /**
42
     * @param iterable $products
43
     * @return static
44
     */
45 4
    public function addItems(iterable $products): self
46
    {
47 4
        foreach ($products as $product) {
48 4
            $this->addItem($product);
49
        }
50
51 4
        return $this;
52
    }
53
54
    /**
55
     * @return Product[]
56
     */
57 37
    public function getItems(): array
58
    {
59 37
        return $this->items;
60
    }
61
62
    /**
63
     * @return Product[]
64
     */
65 38
    public function getUniqueItems(): array
66
    {
67 38
        return array_unique($this->items);
68
    }
69
70
    /**
71
     * The method returns the transfers already paid for the goods
72
     *
73
     * @param Customer $customer
74
     * @param bool|null $gifts
75
     * @return Transfer[]
76
     */
77 38
    public function alreadyBuy(Customer $customer, bool $gifts = null): array
78
    {
79 38
        $status = [Transfer::STATUS_PAID];
80 38
        if ($gifts) {
81 8
            $status[] = Transfer::STATUS_GIFT;
82
        }
83
84
        /**
85
         * @var Transfer $query
86
         */
87 38
        $result = [];
88 38
        $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

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