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

ProgressivePrice   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Importance

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

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getThresholds() 0 3 1
A calculatePrice() 0 33 5
A __construct() 0 11 1
A calculateUsage() 0 9 2
A calculateSum() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace hiqdev\php\billing\price;
6
7
use hiqdev\php\billing\plan\PlanInterface;
8
use hiqdev\php\billing\target\TargetInterface;
9
use hiqdev\php\billing\type\TypeInterface;
10
use hiqdev\php\units\Quantity;
11
use hiqdev\php\units\QuantityInterface;
12
use Money\Currency;
13
use Money\Money;
14
15
class ProgressivePrice extends SinglePrice
16
{
17
    protected ProgressivePriceThresholds $thresholds;
18
19
    public function __construct(
20
        $id,
21
        TypeInterface $type,
22
        TargetInterface $target,
23
        QuantityInterface $prepaid,
24
        Money $price,
25
        array $thresholds,
26
        ?PlanInterface $plan = null
27
    ) {
28
        parent::__construct($id, $type, $target, $plan, $prepaid, $price);
29
        $this->thresholds = new ProgressivePriceThresholds($thresholds);
30
    }
31
32
    /**
33
     * @return ProgressivePriceThresholds
34
     */
35
    public function getThresholds(): array
36
    {
37
        return $this->thresholds;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->thresholds returns the type hiqdev\php\billing\price...gressivePriceThresholds which is incompatible with the type-hinted return array.
Loading history...
38
    }
39
40
    /**
41
     * @inheritDoc
42
     */
43
    public function calculateUsage(QuantityInterface $quantity): ?QuantityInterface
44
    {
45
        $usage = $quantity->convert($this->prepaid->getUnit());
46
47
        if ($usage->isPositive()) {
48
            return $usage;
49
        }
50
51
        return Quantity::create($this->prepaid->getUnit()->getName(), $quantity->getQuantity());
52
    }
53
54
    /**
55
     * @inheritDoc
56
     */
57
    public function calculatePrice(QuantityInterface $quantity): Money
58
    {
59
        $result = new Money(0, $this->price->getCurrency());
60
        $usage = $this->calculateUsage($quantity);
61
        $thresholds = $this->thresholds->get();
62
        $usageCur = null;
63
        foreach ($thresholds as $key => $threshold) {
64
            if (is_null($usageCur)) {
65
                $usageCur = PriceHelper::buildQuantityByMoneyPrice(
66
                    $threshold->getBasePrice(),
67
                    $usage->getUnit()->getName(),
68
                    (string)$usage->getQuantity()
69
                );
70
            }
71
            if  ($threshold->quantity()->compare($usageCur) < 0) {
72
                if ($key !== count($thresholds) - 1) {
73
                    $boundary = $usageCur->subtract($threshold->quantity());
74
                    $result = $result->add(new Money(
75
                            $boundary->multiply($threshold->price()->getAmount())->getQuantity(),
76
                            $threshold->price()->getCurrency()
77
                        )
78
                    );
79
                    $usageCur = $usageCur->subtract($boundary);
80
                } else {
81
                    $result = $result->add(new Money(
82
                            (int) $usage->multiply($threshold->price()->getAmount())->getQuantity(),
83
                            $threshold->price()->getCurrency()
84
                        )
85
                    );
86
                }
87
            }
88
        }
89
        return $result;
90
    }
91
92
    public function calculateSum(QuantityInterface $quantity): ?Money
93
    {
94
        return $this->calculatePrice($quantity);
95
    }
96
}
97