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

ProgressivePriceThreshold   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 56
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
eloc 20
c 1
b 0
f 0
dl 0
loc 56
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A price() 0 3 1
A __construct() 0 10 2
A createFromObjects() 0 7 1
A getBasePrice() 0 3 1
A quantity() 0 3 1
A createFromScalar() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace hiqdev\php\billing\price;
6
7
use Money\Money;
8
use hiqdev\php\units\Quantity;
9
10
class ProgressivePriceThreshold
11
{
12
    private string $price;
13
14
    private string $currency;
15
16
    private string $quantity;
17
18
    private string $unit;
19
20
    private function __construct(string $price, string $currency, string $quantity, string $unit)
21
    {
22
        if ($quantity < 0) {
23
            throw new \InvalidArgumentException('Quantity of the progressive price threshold must be positive');
24
        }
25
26
        $this->price = $price;
27
        $this->currency = $currency;
28
        $this->quantity = $quantity;
29
        $this->unit = $unit;
30
    }
31
32
    public static function createFromScalar(string $price, string $currency, string $quantity, string $unit): self
33
    {
34
        return new self($price, $currency, $quantity, $unit);
35
    }
36
37
    public static function createFromObjects(Money $price, Quantity $quantity)
38
    {
39
        return new self(
40
            $price->getAmount(),
41
            $price->getCurrency()->getCode(),
42
            $quantity->getQuantity(),
43
            $quantity->getUnit()->getName()
44
        );
45
    }
46
47
    /**
48
     * @return Money
49
     */
50
    public function price(): Money
51
    {
52
        return PriceHelper::buildMoney($this->price, $this->currency);
53
    }
54
55
    /**
56
     * @return Quantity
57
     */
58
    public function quantity(): Quantity
59
    {
60
        return PriceHelper::buildQuantityByMoneyPrice($this->price, $this->unit, $this->quantity);
61
    }
62
63
    public function getBasePrice(): string
64
    {
65
        return $this->price;
66
    }
67
}
68