Completed
Pull Request — master (#40)
by Бабичев
07:20 queued 01:42
created

Cart::alreadyBuy()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
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 9
    public function alreadyBuy(Customer $customer, bool $gifts = null): array
85
    {
86 9
        return $customer->paidCart($this, $gifts);
87
    }
88
89
    /**
90
     * @param Customer $customer
91
     * @param bool|null $force
92
     * @return bool
93
     */
94 12
    public function canBuy(Customer $customer, bool $force = null): bool
95
    {
96 12
        foreach ($this->items as $item) {
97 12
            if (!$item->canBuy($customer, $this->getQuantity($item), $force)) {
98 12
                return false;
99
            }
100
        }
101
102 12
        return true;
103
    }
104
105
    /**
106
     * @return int
107
     */
108 1
    public function getTotal(): int
109
    {
110 1
        $result = 0;
111 1
        foreach ($this->items as $item) {
112 1
            $result += $item->getAmountProduct();
113
        }
114 1
        return $result;
115
    }
116
117
    /**
118
     * @return int
119
     */
120 9
    public function count(): int
121
    {
122 9
        return count($this->items);
123
    }
124
125
    /**
126
     * @param Product $product
127
     * @return int
128
     */
129 14
    public function getQuantity(Product $product): int
130
    {
131 14
        $class = get_class($product);
132 14
        $uniq = $product->getUniqueId();
133 14
        return $this->quantity[$class][$uniq] ?? 0;
134
    }
135
136
    /**
137
     * @param Product $product
138
     * @param int $quantity
139
     */
140 14
    protected function addQuantity(Product $product, int $quantity): void
141
    {
142 14
        $class = get_class($product);
143 14
        $uniq = $product->getUniqueId();
144 14
        $this->quantity[$class][$uniq] = $this->getQuantity($product) + $quantity;
145 14
    }
146
147
}
148