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

Cart::getItems()   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
nc 1
nop 0
dl 0
loc 3
ccs 0
cts 3
cp 0
crap 2
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\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