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

Cart::getItems()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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\Product;
7
use Bavix\Wallet\Models\Transfer;
8
use Illuminate\Database\Eloquent\ModelNotFoundException;
9
10
class Cart implements \Countable
11
{
12
13
    /**
14
     * @var Product[]
15
     */
16
    protected $items = [];
17
18
    /**
19
     * @return static
20
     */
21 11
    public static function make(): self
22
    {
23 11
        return new static();
24
    }
25
26
    /**
27
     * @param Product $product
28
     * @return static
29
     */
30 11
    public function addItem(Product $product): self
31
    {
32 11
        $this->items[] = $product;
33 11
        return $this;
34
    }
35
36
    /**
37
     * @param iterable $products
38
     * @return static
39
     */
40 1
    public function addItems(iterable $products): self
41
    {
42 1
        foreach ($products as $product) {
43 1
            $this->addItem($product);
44
        }
45
46 1
        return $this;
47
    }
48
49
    /**
50
     * @return Product[]
51
     */
52 11
    public function getItems(): array
53
    {
54 11
        return $this->items;
55
    }
56
57
    /**
58
     * @param Customer $customer
59
     * @param bool|null $gifts
60
     * @return Transfer[]
61
     */
62 7
    public function hasPaid(Customer $customer, bool $gifts = null): array
63
    {
64 7
        $results = [];
65 7
        foreach ($this->getItems() as $item) {
66 7
            $transfer = $customer->paid($item, $gifts);
67 7
            $results[] = $transfer;
68 7
            if (!$transfer) {
69 2
                throw (new ModelNotFoundException())
70 7
                    ->setModel($customer->transfers()->getMorphClass());
0 ignored issues
show
Bug introduced by
The method transfers() does not exist on Bavix\Wallet\Interfaces\Customer. Did you maybe mean transfer()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

70
                    ->setModel($customer->/** @scrutinizer ignore-call */ transfers()->getMorphClass());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
71
            }
72
        }
73
74 7
        return $results;
75
    }
76
77
    /**
78
     * @param Customer $customer
79
     * @param bool|null $force
80
     * @return bool
81
     */
82 10
    public function canBuy(Customer $customer, bool $force = null): bool
83
    {
84 10
        foreach ($this->items as $item) {
85 10
            if (!$item->canBuy($customer, $force)) {
86 10
                return false;
87
            }
88
        }
89
        
90 10
        return true;
91
    }
92
93
    /**
94
     * @return int
95
     */
96 1
    public function getTotal(): int
97
    {
98 1
        $result = 0;
99 1
        foreach ($this->items as $item) {
100 1
            $result += $item->getAmountProduct();
101
        }
102 1
        return $result;
103
    }
104
105
    /**
106
     * @return int
107
     */
108 1
    public function count(): int
109
    {
110 1
        return \count($this->items);
111
    }
112
113
}
114