Passed
Push — master ( e39bb0...54e3cc )
by Dmitry
14:48
created

ProgressivePriceThreshold::price()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace hiqdev\php\billing\price;
6
7
use InvalidArgumentException;
8
use JsonSerializable;
9
use hiqdev\php\units\Unit;
10
use Money\Money;
11
use hiqdev\php\units\Quantity;
12
13
class ProgressivePriceThreshold implements JsonSerializable
14
{
15
    private string $price;
16
17
    private string $currency;
18
19
    private string $quantity;
20
21
    private string $unit;
22
23
    private function __construct(string $price, string $currency, string $quantity, string $unit)
24
    {
25
        if ($quantity < 0) {
26
            throw new InvalidArgumentException('Quantity of the progressive price threshold must be positive');
27
        }
28
29
        $this->price = $price;
30
        $this->currency = strtoupper($currency);
31
        $this->quantity = $quantity;
32
        $this->unit = $unit;
33
    }
34
35
    public static function createFromScalar(string $price, string $currency, string $quantity, string $unit): self
36
    {
37
        return new self($price, $currency, $quantity, $unit);
38
    }
39
40
    public static function createFromObjects(Money $price, Quantity $quantity)
41
    {
42
        return new self(
43
            $price->getAmount(),
44
            $price->getCurrency()->getCode(),
45
            $quantity->getQuantity(),
46
            $quantity->getUnit()->getName()
47
        );
48
    }
49
50
    public function price(): ?Money
51
    {
52
        return MoneyBuilder::buildMoney($this->price, $this->currency);
53
    }
54
55
    public function quantity(): Quantity
56
    {
57
        return Quantity::create(Unit::create($this->unit), $this->quantity);
0 ignored issues
show
Bug introduced by
hiqdev\php\units\Unit::create($this->unit) of type hiqdev\php\units\Unit is incompatible with the type string expected by parameter $unit of hiqdev\php\units\AbstractQuantity::create(). ( Ignorable by Annotation )

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

57
        return Quantity::create(/** @scrutinizer ignore-type */ Unit::create($this->unit), $this->quantity);
Loading history...
58
    }
59
60
    public function getBasePrice(): string
61
    {
62
        return $this->price;
63
    }
64
65
    public function __toArray(): array
66
    {
67
        return [
68
            'price' => $this->price,
69
            'currency' => $this->currency,
70
            'quantity' => $this->quantity,
71
            'unit' => $this->unit,
72
        ];
73
    }
74
75
    #[\ReturnTypeWillChange]
76
    public function jsonSerialize()
77
    {
78
        return get_object_vars($this);
79
    }
80
}
81