Completed
Push — master ( 963355...94aa33 )
by Joachim
12:06
created

Price::copyProperties()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 1
dl 0
loc 10
ccs 0
cts 5
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", "product_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(name="product_id", 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(null, 'amount')->greaterThan(0);
131
        Assert::that($this->avance)->integer(null, 'avance');
132
        Assert::that($this->b2bGroupId)->string(null, 'b2bGroupId');
133
        Assert::that($this->currency)->isInstanceOf(CurrencyInterface::class, null, 'currency');
134
        Assert::that($this->specialOfferPrice)->integer(null, 'specialOfferPrice')->greaterOrEqualThan(0, null, 'specialOfferPrice');
135
        Assert::that($this->unitPrice)->integer(null, 'unitPrice')->greaterOrEqualThan(0, null, 'unitPrice');
136
        Assert::thatNullOr($this->period)->isInstanceOf(PeriodInterface::class, null, 'period');
137
        Assert::that($this->product)->isInstanceOf(ProductInterface::class);
138
    }
139
140
    /**
141
     * Will copy properties from $price
142
     *
143
     * @param PriceInterface $price
144
     */
145
    public function copyProperties(PriceInterface $price) : void
146
    {
147
        $this->amount = $price->getAmount();
148
        $this->avance = $price->getAvance();
149
        $this->b2bGroupId = $price->getB2bGroupId();
150
        $this->currency = $price->getCurrency();
151
        $this->specialOfferPrice = $price->getSpecialOfferPrice();
152
        $this->unitPrice = $price->getUnitPrice();
153
        $this->period = $price->getPeriod();
154
        $this->product = $price->getProduct();
155
    }
156
157
    /**
158
     * @return int
159
     */
160
    public function getId(): int
161
    {
162
        return (int)$this->id;
163
    }
164
165
    /**
166
     * @param int $id
167
     * @return PriceInterface
168
     */
169
    public function setId(int $id) : PriceInterface
170
    {
171
        $this->id = $id;
172
        return $this;
173
    }
174
175
    /**
176
     * @return int|null
177
     */
178
    public function getAmount() : ?int
179
    {
180
        return $this->amount;
181
    }
182
183
    /**
184
     * @param int|null $amount
185
     * @return PriceInterface
186
     */
187
    public function setAmount(int $amount) : PriceInterface
188
    {
189
        $this->amount = $amount;
190
        return $this;
191
    }
192
193
    /**
194
     * @return int|null
195
     */
196
    public function getAvance() : ?int
197
    {
198
        return $this->avance;
199
    }
200
201
    /**
202
     * @param int|null $avance
203
     * @return PriceInterface
204
     */
205
    public function setAvance(int $avance) : PriceInterface
206
    {
207
        $this->avance = $avance;
208
        return $this;
209
    }
210
211
    /**
212
     * @return null|string
213
     */
214
    public function getB2bGroupId() : ?string
215
    {
216
        return $this->b2bGroupId;
217
    }
218
219
    /**
220
     * @param null|string $b2bGroupId
221
     * @return PriceInterface
222
     */
223
    public function setB2bGroupId(string $b2bGroupId) : PriceInterface
224
    {
225
        $this->b2bGroupId = $b2bGroupId;
226
        return $this;
227
    }
228
229
    /**
230
     * @return null|CurrencyInterface
231
     */
232
    public function getCurrency() : ?CurrencyInterface
233
    {
234
        return $this->currency;
235
    }
236
237
    /**
238
     * @param null|CurrencyInterface $currency
239
     * @return PriceInterface
240
     */
241
    public function setCurrency(CurrencyInterface $currency) : PriceInterface
242
    {
243
        $this->currency = $currency;
244
        return $this;
245
    }
246
247
    /**
248
     * @return Money|null
249
     */
250
    public function getSpecialOfferPrice() : ?Money
251
    {
252
        if(!$this->currency) {
253
            return null;
254
        }
255
        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

255
        return /** @scrutinizer ignore-call */ DandomainFoundation\createMoney($this->currency->getIsoCodeAlpha(), (int)$this->specialOfferPrice);
Loading history...
256
    }
257
258
    /**
259
     * @param Money|null $specialOfferPrice
260
     * @return PriceInterface
261
     */
262
    public function setSpecialOfferPrice(Money $specialOfferPrice) : PriceInterface
263
    {
264
        // @todo change type from int to string
265
        $this->specialOfferPrice = (int)$specialOfferPrice->getAmount();
266
267
        return $this;
268
    }
269
270
    /**
271
     * @return Money|null
272
     */
273
    public function getUnitPrice() : ?Money
274
    {
275
        if(!$this->currency) {
276
            return null;
277
        }
278
        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

278
        return /** @scrutinizer ignore-call */ DandomainFoundation\createMoney($this->currency->getIsoCodeAlpha(), (int)$this->unitPrice);
Loading history...
279
    }
280
281
    /**
282
     * @param Money|null $unitPrice
283
     * @return PriceInterface
284
     */
285
    public function setUnitPrice(Money $unitPrice) : PriceInterface
286
    {
287
        // @todo change type from int to string
288
        $this->unitPrice = (int)$unitPrice->getAmount();
289
290
        return $this;
291
    }
292
293
    /**
294
     * @return Period|null
295
     */
296
    public function getPeriod() : ?PeriodInterface
297
    {
298
        return $this->period;
299
    }
300
301
    /**
302
     * @param PeriodInterface|null $period
303
     * @return PriceInterface
304
     */
305
    public function setPeriod(?PeriodInterface $period) : PriceInterface
306
    {
307
        $this->period = $period;
308
        return $this;
309
    }
310
311
    /**
312
     * @return ProductInterface
313
     */
314
    public function getProduct() : ?ProductInterface
315
    {
316
        return $this->product;
317
    }
318
319
    /**
320
     * @param ProductInterface|null $product
321
     * @return PriceInterface
322
     */
323
    public function setProduct(?ProductInterface $product) : PriceInterface
324
    {
325
        $this->product = $product;
326
        return $this;
327
    }
328
}
329