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

Cart   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 70
ccs 0
cts 38
cp 0
rs 10
c 0
b 0
f 0
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getMetaProduct() 0 15 4
A addItem() 0 3 1
A getAmountProduct() 0 7 2
A canBuy() 0 9 3
A getItems() 0 3 1
1
<?php
2
3
namespace Bavix\Wallet\Objects;
4
5
use Bavix\Wallet\Interfaces\Customer;
6
use Bavix\Wallet\Interfaces\Group;
7
use Bavix\Wallet\Interfaces\Product;
8
9
class Cart implements \Bavix\Wallet\Interfaces\Cart, Group
10
{
11
12
    /**
13
     * @var Product[]
14
     */
15
    protected $items = [];
16
17
    /**
18
     * @inheritDoc
19
     */
20
    public function addItem(Product $product): void
21
    {
22
        $this->items[] = $product;
23
    }
24
25
    /**
26
     * @inheritDoc
27
     */
28
    public function getItems(): array
29
    {
30
        return $this->items;
31
    }
32
33
    /**
34
     * @param Customer $customer
35
     * @param bool|null $force
36
     * @return bool
37
     */
38
    public function canBuy(Customer $customer, bool $force = null): bool
39
    {
40
        foreach ($this->items as $item) {
41
            if (!$item->canBuy($customer, $force)) {
42
                return false;
43
            }
44
        }
45
        
46
        return true;
47
    }
48
49
    /**
50
     * @return int
51
     */
52
    public function getAmountProduct(): int
53
    {
54
        $result = 0;
55
        foreach ($this->items as $item) {
56
            $result += $item->getAmountProduct();
57
        }
58
        return $result;
59
    }
60
61
    /**
62
     * @return array|null
63
     */
64
    public function getMetaProduct(): ?array
65
    {
66
        $meta = [];
67
        foreach ($this->items as $item) {
68
            $data = $item->getMetaProduct();
69
            if ($data) {
70
                $meta[] = $data;
71
            }
72
        }
73
74
        if (empty($meta)) {
75
            return null;
76
        }
77
78
        return $meta;
79
    }
80
81
}
82