ProgressivePriceThreshold   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 74
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 10
eloc 28
c 1
b 0
f 0
dl 0
loc 74
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A price() 0 3 1
A jsonSerialize() 0 4 1
A __construct() 0 10 2
A createFromObjects() 0 7 1
A quantity() 0 3 1
A __toArray() 0 7 1
A createFromScalar() 0 3 1
A unit() 0 3 1
A getRawPrice() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace hiqdev\php\billing\price;
6
7
use hiqdev\php\billing\Money\MultipliedMoney;
8
use hiqdev\php\units\Unit;
9
use hiqdev\php\units\UnitInterface;
10
use InvalidArgumentException;
11
use JsonSerializable;
12
use Money\Money;
13
use hiqdev\php\units\Quantity;
14
15
class ProgressivePriceThreshold implements JsonSerializable
16
{
17
    /**
18
     * @var numeric-string $price The price of the progressive price threshold in currency (not cents)
0 ignored issues
show
Documentation Bug introduced by
The doc comment numeric-string at position 0 could not be parsed: Unknown type name 'numeric-string' at position 0 in numeric-string.
Loading history...
19
     */
20
    private string $price;
21
22
    private string $currency;
23
24
    private string $quantity;
25
26
    private string $unit;
27
28
    private function __construct(string $price, string $currency, string $quantity, string $unit)
29
    {
30
        if ($quantity < 0) {
31
            throw new InvalidArgumentException('Quantity of the progressive price threshold must be positive');
32
        }
33
34
        $this->price = $price;
35
        $this->currency = strtoupper($currency);
36
        $this->quantity = $quantity;
37
        $this->unit = $unit;
38
    }
39
40
    public static function createFromScalar(string $price, string $currency, string $quantity, string $unit): self
41
    {
42
        return new self($price, $currency, $quantity, $unit);
43
    }
44
45
    public static function createFromObjects(Money $price, Quantity $quantity): self
46
    {
47
        return new self(
48
            (string)((int)$price->getAmount() / 100), // TODO: Might be not 100 for some currencies
49
            $price->getCurrency()->getCode(),
50
            (string)$quantity->getQuantity(),
51
            $quantity->getUnit()->getName()
52
        );
53
    }
54
55
    public function price(): ?MultipliedMoney
56
    {
57
        return MultipliedMoney::create($this->price, $this->currency);
58
    }
59
60
    public function quantity(): Quantity
61
    {
62
        return Quantity::create($this->unit, $this->quantity);
63
    }
64
65
    public function unit(): UnitInterface
66
    {
67
        return Unit::create($this->unit);
68
    }
69
70
    public function getRawPrice(): string
71
    {
72
        return $this->price;
73
    }
74
75
    public function __toArray(): array
76
    {
77
        return [
78
            'price' => $this->price,
79
            'currency' => $this->currency,
80
            'quantity' => $this->quantity,
81
            'unit' => $this->unit,
82
        ];
83
    }
84
85
    #[\ReturnTypeWillChange]
86
    public function jsonSerialize()
87
    {
88
        return $this->__toArray();
89
    }
90
}
91