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

61
        return Quantity::create(/** @scrutinizer ignore-type */ Unit::create($this->unit), $this->quantity);
Loading history...
62
    }
63
64
    public function getBasePrice(): string
65
    {
66
        return $this->price;
67
    }
68
}
69