Completed
Push — master ( 778cf4...326520 )
by Joachim
13:13
created

Price::setUnitPrice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Loevgaard\DandomainFoundation\Entity;
4
5
use Assert\Assert;
6
use Doctrine\ORM\Mapping as ORM;
7
use Loevgaard\DandomainFoundation;
8
use Loevgaard\DandomainFoundation\Entity\Generated\ProductInterface;
0 ignored issues
show
Bug introduced by
The type Loevgaard\DandomainFound...erated\ProductInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
9
use Loevgaard\DandomainFoundation\Entity\Generated\PeriodInterface;
0 ignored issues
show
Bug introduced by
The type Loevgaard\DandomainFound...nerated\PeriodInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
10
use Loevgaard\DandomainFoundation\Entity\Generated\CurrencyInterface;
0 ignored issues
show
Bug introduced by
The type Loevgaard\DandomainFound...rated\CurrencyInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use Loevgaard\DandomainFoundation\Entity\Generated\PriceInterface;
0 ignored issues
show
Bug introduced by
The type Loevgaard\DandomainFound...enerated\PriceInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Loevgaard\DandomainFoundation\Entity\Generated\PriceTrait;
0 ignored issues
show
Bug introduced by
The type Loevgaard\DandomainFound...ty\Generated\PriceTrait was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
13
use Money\Money;
14
15
/**
16
 * @ORM\Entity()
17
 * @ORM\Table(name="ldf_prices", uniqueConstraints={@ORM\UniqueConstraint(columns={"amount", "b2b_group_id", "currency_id"})})
18
 * @ORM\HasLifecycleCallbacks()
19
 */
20
class Price extends AbstractEntity implements PriceInterface
21
{
22
    use PriceTrait;
23
24
    /**
25
     * @var int
26
     *
27
     * @ORM\Id
28
     * @ORM\GeneratedValue
29
     * @ORM\Column(type="integer")
30
     **/
31
    protected $id;
32
33
    /**
34
     * @var int|null
35
     *
36
     * @ORM\Column(name="amount", type="integer")
37
     */
38
    protected $amount;
39
40
    /**
41
     * @var int|null
42
     *
43
     * @ORM\Column(type="integer")
44
     */
45
    protected $avance;
46
47
    /**
48
     * @var string|null
49
     *
50
     * @ORM\Column(name="b2b_group_id", type="string", length=191)
51
     */
52
    protected $b2bGroupId;
53
54
    /**
55
     * The currency code in the Dandomain API refers in fact to the currencies' field named 'id' or 'code'
56
     * Therefore we don't have a currencyCode and isoCode property, but a currency property
57
     *
58
     * @var CurrencyInterface|null
59
     *
60
     * @ORM\ManyToOne(targetEntity="Currency")
61
     * @ORM\JoinColumn(name="currency_id", nullable=false)
62
     */
63
    protected $currency;
64
65
    /**
66
     * @var int|null
67
     *
68
     * @ORM\Column(type="integer")
69
     */
70
    protected $specialOfferPrice;
71
72
    /**
73
     * @var int|null
74
     *
75
     * @ORM\Column(type="integer")
76
     */
77
    protected $unitPrice;
78
79
    /**
80
     * @var PeriodInterface|null
81
     *
82
     * @ORM\JoinColumn(onDelete="SET NULL", nullable=true)
83
     * @ORM\ManyToOne(targetEntity="Period")
84
     */
85
    protected $period;
86
87
    /**
88
     * @var ProductInterface
89
     *
90
     * @ORM\ManyToOne(targetEntity="Product", inversedBy="prices")
91
     * @ORM\JoinColumn(nullable=false, onDelete="CASCADE")
92
     */
93
    protected $product;
94
95
    /**
96
     * Creates a valid Price
97
     *
98
     * @param int $amount
99
     * @param int $avance
100
     * @param string $b2bGroupId
101
     * @param CurrencyInterface $currency
102
     * @param int $specialOfferPrice In cents/ører (in danish)
103
     * @param int $unitPrice In cents/ører (in danish)
104
     * @return PriceInterface
105
     */
106
    public static function create(int $amount, int $avance, string $b2bGroupId, CurrencyInterface $currency, int $specialOfferPrice, int $unitPrice) : PriceInterface
107
    {
108
        $specialOfferPrice = new Money($specialOfferPrice, new \Money\Currency($currency->getIsoCodeAlpha()));
109
        $unitPrice = new Money($unitPrice, new \Money\Currency($currency->getIsoCodeAlpha()));
110
111
        $price = new Price();
112
        $price
113
            ->setAmount($amount)
114
            ->setAvance($avance)
115
            ->setB2bGroupId($b2bGroupId)
116
            ->setCurrency($currency)
117
            ->setSpecialOfferPrice($specialOfferPrice)
118
            ->setUnitPrice($unitPrice)
119
        ;
120
121
        return $price;
122
    }
123
124
    /**
125
     * @ORM\PreUpdate()
126
     * @ORM\PrePersist()
127
     */
128
    public function validate()
129
    {
130
        Assert::that($this->amount)->integer()->greaterThan(0);
131
        Assert::that($this->avance)->integer();
132
        Assert::that($this->b2bGroupId)->string();
133
        Assert::that($this->currency)->isInstanceOf(CurrencyInterface::class);
134
        Assert::that($this->specialOfferPrice)->integer()->greaterOrEqualThan(0);
135
        Assert::that($this->unitPrice)->integer()->greaterOrEqualThan(0);
136
        Assert::thatNullOr($this->period)->isInstanceOf(PeriodInterface::class);
137
    }
138
139
    /**
140
     * @return int
141
     */
142
    public function getId(): int
143
    {
144
        return (int)$this->id;
145
    }
146
147
    /**
148
     * @param int $id
149
     * @return PriceInterface
150
     */
151
    public function setId(int $id) : PriceInterface
152
    {
153
        $this->id = $id;
154
        return $this;
155
    }
156
157
    /**
158
     * @return int|null
159
     */
160
    public function getAmount() : ?int
161
    {
162
        return $this->amount;
163
    }
164
165
    /**
166
     * @param int|null $amount
167
     * @return PriceInterface
168
     */
169
    public function setAmount(int $amount) : PriceInterface
170
    {
171
        $this->amount = $amount;
172
        return $this;
173
    }
174
175
    /**
176
     * @return int|null
177
     */
178
    public function getAvance() : ?int
179
    {
180
        return $this->avance;
181
    }
182
183
    /**
184
     * @param int|null $avance
185
     * @return PriceInterface
186
     */
187
    public function setAvance(int $avance) : PriceInterface
188
    {
189
        $this->avance = $avance;
190
        return $this;
191
    }
192
193
    /**
194
     * @return null|string
195
     */
196
    public function getB2bGroupId() : ?string
197
    {
198
        return $this->b2bGroupId;
199
    }
200
201
    /**
202
     * @param null|string $b2bGroupId
203
     * @return PriceInterface
204
     */
205
    public function setB2bGroupId(string $b2bGroupId) : PriceInterface
206
    {
207
        $this->b2bGroupId = $b2bGroupId;
208
        return $this;
209
    }
210
211
    /**
212
     * @return null|CurrencyInterface
213
     */
214
    public function getCurrency() : ?CurrencyInterface
215
    {
216
        return $this->currency;
217
    }
218
219
    /**
220
     * @param null|CurrencyInterface $currency
221
     * @return PriceInterface
222
     */
223
    public function setCurrency(CurrencyInterface $currency) : PriceInterface
224
    {
225
        $this->currency = $currency;
226
        return $this;
227
    }
228
229
    /**
230
     * @return Money|null
231
     */
232
    public function getSpecialOfferPrice() : ?Money
233
    {
234
        if(!$this->currency) {
235
            return null;
236
        }
237
        return DandomainFoundation\createMoney($this->currency->getIsoCodeAlpha(), (int)$this->specialOfferPrice);
0 ignored issues
show
Bug introduced by
The function createMoney was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

237
        return /** @scrutinizer ignore-call */ DandomainFoundation\createMoney($this->currency->getIsoCodeAlpha(), (int)$this->specialOfferPrice);
Loading history...
238
    }
239
240
    /**
241
     * @param Money|null $specialOfferPrice
242
     * @return PriceInterface
243
     */
244
    public function setSpecialOfferPrice(Money $specialOfferPrice) : PriceInterface
245
    {
246
        $this->specialOfferPrice = $specialOfferPrice->getAmount();
0 ignored issues
show
Documentation Bug introduced by
It seems like $specialOfferPrice->getAmount() of type string is incompatible with the declared type null|integer of property $specialOfferPrice.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
247
248
        return $this;
249
    }
250
251
    /**
252
     * @return Money|null
253
     */
254
    public function getUnitPrice() : ?Money
255
    {
256
        if(!$this->currency) {
257
            return null;
258
        }
259
        return DandomainFoundation\createMoney($this->currency->getIsoCodeAlpha(), (int)$this->unitPrice);
0 ignored issues
show
Bug introduced by
The function createMoney was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

259
        return /** @scrutinizer ignore-call */ DandomainFoundation\createMoney($this->currency->getIsoCodeAlpha(), (int)$this->unitPrice);
Loading history...
260
    }
261
262
    /**
263
     * @param Money|null $unitPrice
264
     * @return PriceInterface
265
     */
266
    public function setUnitPrice(Money $unitPrice) : PriceInterface
267
    {
268
        $this->unitPrice = $unitPrice->getAmount();
0 ignored issues
show
Documentation Bug introduced by
It seems like $unitPrice->getAmount() of type string is incompatible with the declared type null|integer of property $unitPrice.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
269
270
        return $this;
271
    }
272
273
    /**
274
     * @return Period|null
275
     */
276
    public function getPeriod() : ?PeriodInterface
277
    {
278
        return $this->period;
279
    }
280
281
    /**
282
     * @param PeriodInterface|null $period
283
     * @return PriceInterface
284
     */
285
    public function setPeriod(PeriodInterface $period) : PriceInterface
286
    {
287
        $this->period = $period;
288
        return $this;
289
    }
290
291
    /**
292
     * @return ProductInterface
293
     */
294
    public function getProduct() : ?ProductInterface
295
    {
296
        return $this->product;
297
    }
298
299
    /**
300
     * @param ProductInterface|null $product
301
     * @return PriceInterface
302
     */
303
    public function setProduct(?ProductInterface $product) : PriceInterface
304
    {
305
        $this->product = $product;
306
        return $this;
307
    }
308
}
309