RatePrice::calculatePrice()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 7
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 13
rs 10
1
<?php
2
/**
3
 * PHP Billing Library
4
 *
5
 * @link      https://github.com/hiqdev/php-billing
6
 * @package   php-billing
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2017-2020, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\php\billing\price;
12
13
use hiqdev\php\billing\plan\PlanInterface;
14
use hiqdev\php\billing\target\TargetInterface;
15
use hiqdev\php\billing\type\TypeInterface;
0 ignored issues
show
Bug introduced by
The type hiqdev\php\billing\type\TypeInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
16
use hiqdev\php\units\QuantityInterface;
17
use Money\Money;
18
19
/**
20
 * Rate Price:
21
 * - holds rate in percents
22
 * e.g. rate for referral payments calculation
23
 * @see PriceInterface
24
 *
25
 * @author Andrii Vasyliev <[email protected]>
26
 */
27
class RatePrice extends AbstractPrice implements PriceWithRateInterface
28
{
29
    protected float $rate;
30
31
    public function __construct(
32
        $id,
33
        TypeInterface $type,
34
        TargetInterface $target,
35
        ?PlanInterface $plan,
36
        float $rate
37
    ) {
38
        parent::__construct($id, $type, $target, $plan);
39
        $this->rate = $rate;
40
    }
41
42
    public function getRate(): float
43
    {
44
        return $this->rate;
45
    }
46
47
    public function calculateSum(QuantityInterface $quantity): ?Money
48
    {
49
        $sum = $quantity->multiply((string) -$this->rate);
50
        $currency = strtoupper($sum->getUnit()->getName());
51
52
        return Money::{$currency}((int)floor($sum->getQuantity()));
0 ignored issues
show
Bug introduced by
It seems like $sum->getQuantity() can also be of type string; however, parameter $num of floor() does only seem to accept double|integer, maybe add an additional type check? ( Ignorable by Annotation )

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

52
        return Money::{$currency}((int)floor(/** @scrutinizer ignore-type */ $sum->getQuantity()));
Loading history...
53
    }
54
55
    public function calculatePrice(QuantityInterface $quantity): ?Money
56
    {
57
        $sum = $this->calculateSum($quantity);
58
        if ($sum === null) {
59
            return null;
60
        }
61
62
        $usage = $this->calculateUsage($quantity);
63
        if ($usage === null) {
64
            return null;
65
        }
66
67
        return $sum->divide(sprintf('%.14F', $usage->getQuantity()));
68
    }
69
70
    public function calculateUsage(QuantityInterface $quantity): ?QuantityInterface
71
    {
72
        return $quantity;
73
    }
74
}
75