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

Cart::make()   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 Countable;
9
use Illuminate\Database\Eloquent\ModelNotFoundException;
10
use function count;
11
12
class Cart implements Countable
13
{
14
15
    /**
16
     * @var Product[]
17
     */
18
    protected $items = [];
19
20
    /**
21
     * @return static
22
     */
23 11
    public static function make(): self
24
    {
25 11
        return new static();
26
    }
27
28
    /**
29
     * @param Product $product
30
     * @return static
31
     */
32 11
    public function addItem(Product $product): self
33
    {
34 11
        $this->items[] = $product;
35 11
        return $this;
36
    }
37
38
    /**
39
     * @param iterable $products
40
     * @return static
41
     */
42 1
    public function addItems(iterable $products): self
43
    {
44 1
        foreach ($products as $product) {
45 1
            $this->addItem($product);
46
        }
47
48 1
        return $this;
49
    }
50
51
    /**
52
     * @return Product[]
53
     */
54 11
    public function getItems(): array
55
    {
56 11
        return $this->items;
57
    }
58
59
    /**
60
     * @param Customer $customer
61
     * @param bool|null $gifts
62
     * @return Transfer[]
63
     */
64 7
    public function hasPaid(Customer $customer, bool $gifts = null): array
65
    {
66 7
        $results = [];
67 7
        foreach ($this->getItems() as $item) {
68 7
            $transfer = $customer->paid($item, $gifts);
69 7
            $results[] = $transfer;
70 7
            if (!$transfer) {
71 2
                throw (new ModelNotFoundException())
72 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

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