Passed
Pull Request — master (#67)
by
unknown
12:51
created

ProgressivePrice::getIntervalBoundary()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 7
c 1
b 0
f 0
dl 0
loc 9
rs 10
cc 3
nc 3
nop 2
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\Money;
13
14
class ProgressivePrice extends AbstractPrice
15
{
16
17
    public const SIGN_GREATER = '>';
18
    public const SIGN_LESS = '<';
19
    public const SIGN_GREATER_EQUAL = '>=';
20
    public const SIGN_LESS_EQUAL = '<=';
21
    public const SIGN_EQUAL = '=';
22
23
    /* @psalm-var array{array{
24
     * "price": numeric,
25
     * "currency": string,
26
     * "sign_from": string,
27
     * "value_from": numeric,
28
     * "sign_till": string,
29
     * "value_till", numeric
30
     * }} $condition
31
     */
32
    protected array $condition;
33
    /**
34
     * @var QuantityInterface prepaid quantity also implies Unit
35
     * XXX cannot be null cause Unit is required
36
     */
37
    protected $prepaid;
38
39
    public function __construct(
40
        $id,
41
        TypeInterface $type,
42
        TargetInterface $target,
43
        QuantityInterface $prepaid,
44
        array $condition,
45
        PlanInterface $plan = null,
46
    ) {
47
        parent::__construct($id, $type, $target, $plan);
48
        $this->prepaid = $prepaid;
49
        $this->condition = $condition;
50
    }
51
52
    public function getPrepaid()
53
    {
54
        return $this->prepaid;
55
    }
56
57
    public function getCondition(): array
58
    {
59
        return $this->condition;
60
    }
61
62
    public function setCondition(string $key, array $condition): self
0 ignored issues
show
Unused Code introduced by
The parameter $key is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

62
    public function setCondition(/** @scrutinizer ignore-unused */ string $key, array $condition): self

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
63
    {
64
        $this->condition[] = $condition;
65
        return $this;
66
    }
67
68
    private function prepareCondition(): void
69
    {
70
        uksort($this->condition, function($a, $b)
71
        {
72
            return $b['value_till'] - $a['value_till'];
73
        }
74
        );
75
    }
76
77
    /**
78
     * @inheritDoc
79
     */
80
    public function calculateUsage(QuantityInterface $quantity): ?QuantityInterface
81
    {
82
        $usage = $quantity->convert($this->prepaid->getUnit())->subtract($this->prepaid);
83
84
        if ($usage->isPositive()) {
85
            return $usage;
86
        }
87
88
        return Quantity::create($this->prepaid->getUnit()->getName(), 0);
89
    }
90
91
    /**
92
     * @inheritDoc
93
     */
94
    public function calculatePrice(QuantityInterface $quantity): ?Money
95
    {
96
        $result = null;
97
        $this->prepareCondition();
98
        $usage = $this->calculateUsage($quantity);
99
        $quantity = $usage->getQuantity();
0 ignored issues
show
Unused Code introduced by
The assignment to $quantity is dead and can be removed.
Loading history...
100
        foreach ($this->condition as $condition) {
101
            $quantity = $usage->getQuantity();
102
            if (isset($condition['sign_from'])) {
103
                if ($condition['sign_till'] === self::SIGN_EQUAL) {
104
                    $result = $usage->getQuantity();
105
                }
106
            } else {
107
                if ($condition['sign_till'] === self::SIGN_EQUAL || $condition['sign_till'] === self::SIGN_GREATER) {
108
109
                }
110
            }
111
        }
112
        return $result;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $result could return the type double|integer|string which is incompatible with the type-hinted return Money\Money|null. Consider adding an additional type-check to rule them out.
Loading history...
113
    }
114
115
    private function getIntervalBoundary(string $sign, int $value): float
0 ignored issues
show
Unused Code introduced by
The method getIntervalBoundary() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
116
    {
117
        switch ($sign) {
118
            case self::SIGN_GREATER:
119
                return $value + 0.01;
120
            case self::SIGN_LESS:
121
                return $value - 0.01;
122
            default:
123
                return $value;
124
        }
125
    }
126
}
127