Completed
Pull Request — master (#40)
by Бабичев
08:10
created

Cart   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 132
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 32
dl 0
loc 132
ccs 40
cts 40
cp 1
rs 10
c 0
b 0
f 0
wmc 17

10 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 alreadyBuy() 0 11 3
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
12
class Cart implements Countable
13
{
14
15
    /**
16
     * @var Product[]
17
     */
18
    protected $items = [];
19
20
    /**
21
     * @var int[]
22
     */
23
    protected $quantity = [];
24
25
    /**
26
     * @return static
27
     */
28 12
    public static function make(): self
29
    {
30 12
        return new static();
31
    }
32
33
    /**
34
     * @param Product $product
35
     * @param int $quantity
36
     * @return static
37
     */
38 12
    public function addItem(Product $product, int $quantity = 1): self
39
    {
40 12
        $this->addQuantity($product, $quantity);
41 12
        for ($i = 0; $i < $quantity; $i++) {
42 12
            $this->items[] = $product;
43
        }
44 12
        return $this;
45
    }
46
47
    /**
48
     * @param iterable $products
49
     * @return static
50
     */
51 2
    public function addItems(iterable $products): self
52
    {
53 2
        foreach ($products as $product) {
54 2
            $this->addItem($product);
55
        }
56
57 2
        return $this;
58
    }
59
60
    /**
61
     * @return Product[]
62
     */
63 12
    public function getItems(): array
64
    {
65 12
        return $this->items;
66
    }
67
68
    /**
69
     * The method returns the transfers already paid for the goods
70
     *
71
     * @param Customer $customer
72
     * @param bool|null $gifts
73
     * @return Transfer[]
74
     */
75 8
    public function alreadyBuy(Customer $customer, bool $gifts = null): array
76
    {
77 8
        $results = [];
78 8
        foreach ($this->getItems() as $item) {
79 8
            $transfer = $customer->paid($item, $gifts);
80 8
            if ($transfer) {
81 8
                $results[] = $transfer;
82
            }
83
        }
84
85 8
        return $results;
86
    }
87
88
    /**
89
     * @param Customer $customer
90
     * @param bool|null $force
91
     * @return bool
92
     */
93 11
    public function canBuy(Customer $customer, bool $force = null): bool
94
    {
95 11
        foreach ($this->items as $item) {
96 11
            if (!$item->canBuy($customer, $this->getQuantity($item), $force)) {
97 11
                return false;
98
            }
99
        }
100
101 11
        return true;
102
    }
103
104
    /**
105
     * @return int
106
     */
107 1
    public function getTotal(): int
108
    {
109 1
        $result = 0;
110 1
        foreach ($this->items as $item) {
111 1
            $result += $item->getAmountProduct();
112
        }
113 1
        return $result;
114
    }
115
116
    /**
117
     * @return int
118
     */
119 8
    public function count(): int
120
    {
121 8
        return count($this->items);
122
    }
123
124
    /**
125
     * @param Product $product
126
     * @return int
127
     */
128 12
    protected function getQuantity(Product $product): int
129
    {
130 12
        $class = get_class($product);
131 12
        $uniq = $product->getUniqueId();
132 12
        return $this->quantity[$class][$uniq] ?? 0;
133
    }
134
135
    /**
136
     * @param Product $product
137
     * @param int $quantity
138
     */
139 12
    protected function addQuantity(Product $product, int $quantity): void
140
    {
141 12
        $class = get_class($product);
142 12
        $uniq = $product->getUniqueId();
143 12
        $this->quantity[$class][$uniq] = $this->getQuantity($product) + $quantity;
144 12
    }
145
146
}
147