Test Failed
Pull Request — master (#7)
by Svaťa
05:11
created

Item::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Simara\Cart\Domain\Cart;
4
5
use Simara\Cart\Domain\Cart\ItemDetail;
6
use Simara\Cart\Domain\Price;
7
use Simara\Cart\Domain\Prices\Prices;
8
9
class Item
10
{
11
    private $generatedId;
0 ignored issues
show
introduced by
The private property $generatedId is not used, and could be removed.
Loading history...
12
13
    private string $productId;
14
15
    private int $amount;
16
17
    /**
18
     * @throws AmountMustBePositiveException
19
	 */
20
    public function __construct(string $productId, int $amount)
21
    {
22
        $this->checkAmount($amount);
23
        $this->productId = $productId;
24
        $this->amount = $amount;
25
    }
26
27
    public function toDetail(Prices $prices): ItemDetail
28
    {
29
        return new ItemDetail($this->productId, $prices->unitPrice($this->productId), $this->amount);
30
    }
31
32
    public function getProductId(): string
33
    {
34
        return $this->productId;
35
    }
36
37
    /**
38
     * @throws AmountMustBePositiveException
39
	 */
40
    public function add(int $amount): void
41
    {
42
        $this->checkAmount($amount);
43
        $this->amount = $this->amount + $amount;
44
    }
45
46
    /**
47
     * @throws AmountMustBePositiveException
48
	 */
49
    private function checkAmount(int $amount): void
50
    {
51
        if ($amount <= 0) {
52
            throw new AmountMustBePositiveException();
53
        }
54
    }
55
56
    /**
57
     * @throws AmountMustBePositiveException
58
	 */
59
    public function changeAmount(int $amount): void
60
    {
61
        $this->checkAmount($amount);
62
        $this->amount = $amount;
63
    }
64
65
    public function calculatePrice(Prices $prices): Price
66
	{
67
        return $prices->unitPrice($this->productId)->multiply($this->amount);
68
    }
69
}
70