Completed
Push — master ( 4e62cb...ede51b )
by Joachim
29:38 queued 14:39
created

OrderLine::getVariant()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Loevgaard\DandomainFoundation\Entity;
4
5
use Doctrine\ORM\Mapping as ORM;
6
use Loevgaard\DandomainFoundation;
7
use Loevgaard\DandomainFoundation\Entity\Generated\OrderLineInterface;
0 ignored issues
show
Bug introduced by
The type Loevgaard\DandomainFound...ated\OrderLineInterface 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...
8
use Loevgaard\DandomainFoundation\Entity\Generated\OrderLineTrait;
0 ignored issues
show
Bug introduced by
The type Loevgaard\DandomainFound...enerated\OrderLineTrait 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\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...
10
use Money\Money;
11
12
/**
13
 * @ORM\Entity()
14
 * @ORM\Table(name="ldf_order_lines")
15
 */
16
class OrderLine extends AbstractEntity implements OrderLineInterface
17
{
18
    use OrderLineTrait;
19
20
    protected $hydrateConversions = [
21
        'id' => 'externalId',
22
        'productId' => 'productNumber'
23
    ];
24
25
    /**
26
     * @var int
27
     *
28
     * @ORM\Id
29
     * @ORM\GeneratedValue
30
     * @ORM\Column(type="integer")
31
     **/
32
    protected $id;
33
34
    /**
35
     * @var int
36
     *
37
     * @ORM\Column(type="integer", unique=true)
38
     */
39
    protected $externalId;
40
41
    /**
42
     * @var string|null
43
     *
44
     * @ORM\Column(nullable=true, type="string", length=191)
45
     */
46
    protected $fileUrl;
47
48
    /**
49
     * @var int|null
50
     *
51
     * @ORM\Column(nullable=true, type="string", length=191)
52
     */
53
    protected $productNumber;
54
55
    /**
56
     * @var string|null
57
     *
58
     * @ORM\Column(nullable=true, type="text")
59
     */
60
    protected $productName;
61
62
    /**
63
     * @var int|null
64
     *
65
     * @ORM\Column(nullable=true, type="integer")
66
     */
67
    protected $quantity;
68
69
    /**
70
     * This number is excl vat
71
     *
72
     * @var int|null
73
     *
74
     * @ORM\Column(nullable=true, type="integer")
75
     */
76
    protected $unitPrice;
77
78
    /**
79
     * This number is excl vat
80
     *
81
     * @var int|null
82
     *
83
     * @ORM\Column(nullable=true, type="integer")
84
     */
85
    protected $totalPrice;
86
87
    /**
88
     * @var float|null
89
     *
90
     * @ORM\Column(nullable=true, type="decimal", precision=5, scale=2)
91
     */
92
    protected $vatPct;
93
94
    /**
95
     * @var string|null
96
     *
97
     * @ORM\Column(nullable=true, type="string", length=191)
98
     */
99
    protected $variant;
100
101
    /**
102
     * @var string|null
103
     *
104
     * @ORM\Column(nullable=true, type="text")
105
     */
106
    protected $xmlParams;
107
108
    /**
109
     * @var Order
110
     *
111
     * @ORM\JoinColumn(onDelete="CASCADE", nullable=false)
112
     * @ORM\ManyToOne(inversedBy="orderLines", targetEntity="Order")
113
     */
114
    protected $order;
115
116
    /**
117
     * @var ProductInterface|null
118
     *
119
     * @ORM\JoinColumn(onDelete="SET NULL")
120
     * @ORM\ManyToOne(targetEntity="Product", cascade={"persist"})
121
     */
122
    protected $product;
123
124
    // @todo implement withVat and withoutVat methods
125
126 6
    public function hydrate(array $data, bool $useConversions = false, $scalarsOnly = true)
127
    {
128 6
        if (is_null($this->order)) {
129 3
            throw new \RuntimeException('Cannot hydrate order line without an associated order');
130
        }
131
132 3
        if (isset($data['unitPrice'])) {
133
            $data['unitPrice'] = DandomainFoundation\createMoneyFromFloat($this->getCurrencyCode(), $data['unitPrice']);
0 ignored issues
show
Bug introduced by
The function createMoneyFromFloat 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

133
            $data['unitPrice'] = /** @scrutinizer ignore-call */ DandomainFoundation\createMoneyFromFloat($this->getCurrencyCode(), $data['unitPrice']);
Loading history...
134 3
        }
135 3
136
        if (isset($data['totalPrice'])) {
137
            $data['totalPrice'] = DandomainFoundation\createMoneyFromFloat($this->getCurrencyCode(), $data['totalPrice']);
138 3
        }
139 3
140
        parent::hydrate($data, $useConversions, $scalarsOnly);
141
    }
142 3
143 3
    /*
144
     * Helper methods
145
     */
146
    public function getUnitPriceInclVat() : ?Money
147
    {
148
        $unitPrice = $this->getUnitPrice();
149
        if (!$unitPrice) {
150
            return null;
151
        }
152
153
        $multiplier = (100 + $this->vatPct) / 100;
154
155
        return $unitPrice->multiply($multiplier);
156
    }
157
158
    public function getUnitPriceExclVat() : ?Money
159
    {
160
        return $this->getUnitPrice();
161
    }
162
163
    public function getTotalPriceInclVat() : ?Money
164
    {
165
        $totalPrice = $this->getTotalPrice();
166
        if (!$totalPrice) {
167
            return null;
168
        }
169
170
        $multiplier = (100 + $this->vatPct) / 100;
171
172
        return $totalPrice->multiply($multiplier);
173
    }
174
175
    public function getTotalPriceExclVat() : ?Money
176
    {
177
        return $this->getTotalPrice();
178
    }
179
180
    /**
181
     * @return int
182
     */
183
    public function getId(): int
184
    {
185 6
        return (int)$this->id;
186
    }
187 6
188
    /**
189
     * @param int $id
190
     * @return OrderLineInterface
191
     */
192
    public function setId(int $id)
193
    {
194 3
        $this->id = $id;
195
        return $this;
196 3
    }
197 3
198
    /**
199
     * @return int
200
     */
201
    public function getExternalId(): int
202
    {
203 21
        return (int)$this->externalId;
204
    }
205 21
206
    /**
207
     * @param int $externalId
208
     * @return OrderLineInterface
209
     */
210
    public function setExternalId(int $externalId)
211
    {
212 18
        $this->externalId = $externalId;
213
        return $this;
214 18
    }
215 18
216
    /**
217
     * @return null|string
218
     */
219
    public function getFileUrl()
220
    {
221 3
        return $this->fileUrl;
222
    }
223 3
224
    /**
225
     * @param null|string $fileUrl
226
     * @return OrderLineInterface
227
     */
228
    public function setFileUrl($fileUrl)
229
    {
230 3
        $this->fileUrl = $fileUrl;
231
        return $this;
232 3
    }
233 3
234
    /**
235
     * @return int|null
236
     */
237
    public function getProductNumber()
238
    {
239 3
        return $this->productNumber;
240
    }
241 3
242
    /**
243
     * @param int|null $productNumber
244
     * @return OrderLineInterface
245
     */
246
    public function setProductNumber($productNumber)
247
    {
248 3
        $this->productNumber = $productNumber;
249
        return $this;
250 3
    }
251 3
252
    /**
253
     * @return null|string
254
     */
255
    public function getProductName()
256
    {
257 3
        return $this->productName;
258
    }
259 3
260
    /**
261
     * @param null|string $productName
262
     * @return OrderLineInterface
263
     */
264
    public function setProductName($productName)
265
    {
266 3
        $this->productName = $productName;
267
        return $this;
268 3
    }
269 3
270
    /**
271
     * @return int|null
272
     */
273
    public function getQuantity()
274
    {
275 3
        return $this->quantity;
276
    }
277 3
278
    /**
279
     * @param int|null $quantity
280
     * @return OrderLineInterface
281
     */
282
    public function setQuantity($quantity)
283
    {
284 3
        $this->quantity = $quantity;
285
        return $this;
286 3
    }
287 3
288
    /**
289
     * @return Money|null
290
     */
291
    public function getTotalPrice()
292
    {
293 3
        return DandomainFoundation\createMoney($this->getCurrencyCode(), (int)$this->totalPrice);
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

293
        return /** @scrutinizer ignore-call */ DandomainFoundation\createMoney($this->getCurrencyCode(), (int)$this->totalPrice);
Loading history...
294
    }
295 3
296
    /**
297
     * @param Money|null $totalPrice
298
     * @return OrderLineInterface
299
     */
300
    public function setTotalPrice(Money $totalPrice = null)
301
    {
302 3
        $this->totalPrice = $totalPrice ? $totalPrice->getAmount() : $totalPrice;
303
304 3
        return $this;
305
    }
306 3
307
    /**
308
     * @return Money|null
309
     */
310
    public function getUnitPrice()
311
    {
312 3
        return DandomainFoundation\createMoney($this->getCurrencyCode(), (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

312
        return /** @scrutinizer ignore-call */ DandomainFoundation\createMoney($this->getCurrencyCode(), (int)$this->unitPrice);
Loading history...
313
    }
314 3
315
    /**
316
     * @param Money|null $unitPrice
317
     * @return OrderLineInterface
318
     */
319
    public function setUnitPrice(Money $unitPrice = null)
320
    {
321 3
        $this->unitPrice = $unitPrice ? $unitPrice->getAmount() : $unitPrice;
322
        return $this;
323 3
    }
324 3
325
    /**
326
     * @return float|null
327
     */
328
    public function getVatPct()
329
    {
330 3
        return $this->vatPct;
331
    }
332 3
333
    /**
334
     * @param float|null $vatPct
335
     * @return OrderLineInterface
336
     */
337
    public function setVatPct($vatPct)
338
    {
339 3
        $this->vatPct = $vatPct;
340
        return $this;
341 3
    }
342 3
343
    /**
344
     * @return null|string
345
     */
346
    public function getVariant()
347
    {
348 3
        return $this->variant;
349
    }
350 3
351
    /**
352
     * @param null|string $variant
353
     * @return OrderLineInterface
354
     */
355
    public function setVariant($variant)
356
    {
357 3
        $this->variant = $variant;
358
        return $this;
359 3
    }
360 3
361
    /**
362
     * @return null|string
363
     */
364
    public function getXmlParams()
365
    {
366 3
        return $this->xmlParams;
367
    }
368 3
369
    /**
370
     * @param null|string $xmlParams
371
     * @return OrderLineInterface
372
     */
373
    public function setXmlParams($xmlParams)
374
    {
375 3
        $this->xmlParams = $xmlParams;
376
        return $this;
377 3
    }
378 3
379
    /**
380
     * @return Order
381
     */
382
    public function getOrder(): Order
383
    {
384 6
        return $this->order;
385
    }
386 6
387
    /**
388
     * @param Order|null $order
389
     * @return OrderLineInterface
390
     */
391
    public function setOrder(Order $order)
392
    {
393 18
        $this->order = $order;
394
        return $this;
395 18
    }
396 18
397
    /**
398
     * @return ProductInterface|null
399
     */
400
    public function getProduct()
401
    {
402 3
        return $this->product;
403
    }
404 3
405
    /**
406
     * @param ProductInterface|null $product
407
     * @return OrderLineInterface
408
     */
409
    public function setProduct(ProductInterface $product = null)
410
    {
411 3
        $this->product = $product;
412
        return $this;
413 3
    }
414 3
415
    protected function getCurrencyCode() : string
416
    {
417
        return $this->getOrder()->getCurrency()->getIsoCodeAlpha();
418
    }
419
}
420