Completed
Pull Request — master (#40)
by Бабичев
15:20 queued 11:36
created

Cart::getMetaProduct()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 0
Metric Value
cc 4
eloc 8
nc 6
nop 0
dl 0
loc 15
ccs 0
cts 13
cp 0
crap 20
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): Group
21
    {
22
        $this->items[] = $product;
23
        return $this;
24
    }
25
26
    /**
27
     * @inheritDoc
28
     */
29
    public function getItems(): array
30
    {
31
        return $this->items;
32
    }
33
34
    /**
35
     * @param Customer $customer
36
     * @param bool|null $force
37
     * @return bool
38
     */
39
    public function canBuy(Customer $customer, bool $force = null): bool
40
    {
41
        foreach ($this->items as $item) {
42
            if (!$item->canBuy($customer, $force)) {
43
                return false;
44
            }
45
        }
46
        
47
        return true;
48
    }
49
50
    /**
51
     * @return int
52
     */
53
    public function getAmountProduct(): int
54
    {
55
        $result = 0;
56
        foreach ($this->items as $item) {
57
            $result += $item->getAmountProduct();
58
        }
59
        return $result;
60
    }
61
62
    /**
63
     * @return array|null
64
     */
65
    public function getMetaProduct(): ?array
66
    {
67
        $meta = [];
68
        foreach ($this->items as $item) {
69
            $data = $item->getMetaProduct();
70
            if ($data) {
71
                $meta[] = $data;
72
            }
73
        }
74
75
        if (empty($meta)) {
76
            return null;
77
        }
78
79
        return $meta;
80
    }
81
82
}
83