Passed
Pull Request — master (#67)
by
unknown
13:58
created

ProgressivePriceThreshold::getBasePrice()   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 hiqdev\php\units\Unit;
9
use Money\Money;
10
use hiqdev\php\units\Quantity;
11
12
class ProgressivePriceThreshold
13
{
14
    public string $price;
15
16
    public string $currency;
17
18
    public string $quantity;
19
20
    public string $unit;
21
22
    private function __construct(string $price, string $currency, string $quantity, string $unit)
23
    {
24
        if ($quantity < 0) {
25
            throw new InvalidArgumentException('Quantity of the progressive price threshold must be positive');
26
        }
27
28
        $this->price = $price;
29
        $this->currency = strtoupper($currency);
30
        $this->quantity = $quantity;
31
        $this->unit = $unit;
32
    }
33
34
    public static function createFromScalar(string $price, string $currency, string $quantity, string $unit): self
35
    {
36
        return new self($price, $currency, $quantity, $unit);
37
    }
38
39
    public static function createFromObjects(Money $price, Quantity $quantity)
40
    {
41
        return new self(
42
            $price->getAmount(),
43
            $price->getCurrency()->getCode(),
44
            $quantity->getQuantity(),
45
            $quantity->getUnit()->getName()
46
        );
47
    }
48
49
    public function price(): ?Money
50
    {
51
        return MoneyBuilder::buildMoney($this->price, $this->currency);
52
    }
53
54
    public function quantity(): Quantity
55
    {
56
        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

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