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

Cart   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Test Coverage

Coverage 46.15%

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 80
ccs 12
cts 26
cp 0.4615
rs 10
c 0
b 0
f 0
wmc 12

6 Methods

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