Price::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace ConferenceTools\Tickets\Domain\ValueObject;
4
5
use JMS\Serializer\Annotation as Jms;
6
use Doctrine\ORM\Mapping as ORM;
7
8
/**
9
 * Class Money
10
 * @ORM\Embeddable()
11
 */
12
class Price
13
{
14
    /**
15
     * @var Money
16
     * @ORM\Embedded(class="ConferenceTools\Tickets\Domain\ValueObject\Money")
17
     * @Jms\Type("ConferenceTools\Tickets\Domain\ValueObject\Money")
18
     */
19
    private $net;
20
21
    /**
22
     * @var TaxRate
23
     * @ORM\Embedded(class="ConferenceTools\Tickets\Domain\ValueObject\TaxRate")
24
     * @Jms\Type("ConferenceTools\Tickets\Domain\ValueObject\TaxRate")
25
     */
26
    private $taxRate;
27
28 48
    private function __construct(Money $net, TaxRate $taxRate)
29
    {
30 48
        $this->net = $net;
31 48
        $this->taxRate = $taxRate;
32 48
    }
33
34
    /**
35
     * @return Money
36
     */
37 12
    public function getNet(): Money
38
    {
39 12
        return $this->net;
40
    }
41
42
    /**
43
     * @return TaxRate
44
     */
45 9
    public function getTaxRate(): TaxRate
46
    {
47 9
        return $this->taxRate;
48
    }
49
50 2
    public function getGross(): Money
51
    {
52 2
        return $this->taxRate->calculateGross($this->net);
53
    }
54
55 1
    public function getTax(): Money
56
    {
57 1
        return $this->taxRate->calculateTaxFromNet($this->net);
58
    }
59
60 43
    public static function fromNetCost(Money $net, TaxRate $taxRate)
61
    {
62 43
        return new static($net, $taxRate);
63
    }
64
65 1
    public static function fromGrossCost(Money $gross, TaxRate $taxRate)
66
    {
67 1
        return new static($taxRate->calculateNet($gross), $taxRate);
68
    }
69
70
    /**
71
     * @param Price $other
72
     * @return bool
73
     */
74 36
    public function isSameTaxRate(Price $other): bool
75
    {
76 36
        return $this->taxRate->equals($other->taxRate);
77
    }
78
79
    /**
80
     * @throws \InvalidArgumentException
81
     */
82 25
    private function assertSameTaxRate(Price $other)
83
    {
84 25
        if (!$this->isSameTaxRate($other)) {
85 1
            throw new \InvalidArgumentException('Different tax rates');
86
        }
87 24
    }
88
89
    /**
90
     * @param Price $other
91
     * @return bool
92
     */
93 18
    public function equals(Price $other): bool
94
    {
95 18
        return ($this->isSameTaxRate($other) && $other->net->equals($this->net));
96
    }
97
98
    /**
99
     * @param Price $other
100
     * @return int
101
     */
102 7
    public function compare(Price $other): int
103
    {
104 7
        $this->assertSameTaxRate($other);
105 6
        if ($this->net->lessThan($other->net)) {
106 5
            return -1;
107 6
        } elseif ($this->net->equals($other->net)) {
108 1
            return 0;
109
        } else {
110 5
            return 1;
111
        }
112
    }
113
114
    /**
115
     * @param Price $other
116
     * @return bool
117
     */
118 1
    public function greaterThan(Price $other): bool
119
    {
120 1
        return 1 === $this->compare($other);
121
    }
122
123
    /**
124
     * @param Price $other
125
     * @return bool
126
     */
127 1
    public function lessThan(Price $other): bool
128
    {
129 1
        return -1 === $this->compare($other);
130
    }
131
132 17
    public function add(Price $addend): Price
133
    {
134 17
        $this->assertSameTaxRate($addend);
135
136 17
        return new self($this->net->add($addend->net), $this->taxRate);
137
    }
138
139 5
    public function subtract(Price $subtrahend): Price
140
    {
141 5
        $this->assertSameTaxRate($subtrahend);
142
143 5
        return new self($this->net->subtract($subtrahend->net), $this->taxRate);
144
    }
145
146 12
    public function multiply($multiple): Price
147
    {
148 12
        return new self($this->net->multiply($multiple), $this->taxRate);
149
    }
150
}