Issues (113)

src/price/EnumPrice.php (1 issue)

Labels
Severity
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
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 hiqdev\php\units\UnitInterface;
18
use Money\Currency;
19
use Money\Money;
20
21
/**
22
 * Enum Price:
23
 * - holds sums list: amount => total sum for the quantity NOT price per unit
24
 * - listed quantities only else exception.
25
 * @see PriceInterface
26
 *
27
 * @author Andrii Vasyliev <[email protected]>
28
 */
29
class EnumPrice extends AbstractPrice implements PriceWithSumsInterface, PriceWithCurrencyInterface, PriceWithUnitInterface
30
{
31
    protected UnitInterface $unit;
32
33
    protected Currency $currency;
34
35
    protected Sums $sums;
36
37
    public function __construct(
38
        $id,
39
        TypeInterface $type,
40
        TargetInterface $target,
41
        ?PlanInterface $plan,
42
        UnitInterface $unit,
43
        Currency $currency,
44
        Sums $sums,
45
    ) {
46 2
        parent::__construct($id, $type, $target, $plan);
47
        $this->unit = $unit;
48
        $this->currency = $currency;
49
        $this->sums = $sums;
50
    }
51
52
    public function getUnit(): UnitInterface
53
    {
54
        return $this->unit;
55 2
    }
56 2
57 2
    public function getCurrency(): Currency
58 2
    {
59 2
        return $this->currency;
60
    }
61 1
62
    public function getSums(): Sums
63 1
    {
64
        return $this->sums;
65
    }
66 1
67
    /**
68 1
     * {@inheritdoc}
69
     */
70
    public function calculateSum(QuantityInterface $quantity): ?Money
71 1
    {
72
        $usage = $this->calculateUsage($quantity)->getQuantity();
73 1
74
        foreach ($this->sums->values() as $value => $price) {
75
            if ((string) $value === (string) $usage) {
76
                return new Money($price, $this->currency);
77
            }
78
        }
79 4
80
        throw new FailedCalculatePriceException('not enumed quantity: ' . $usage);
81 4
    }
82
83 4
    /**
84 4
     * {@inheritdoc}
85 4
     */
86
    public function calculatePrice(QuantityInterface $quantity): ?Money
87
    {
88
        $sum = $this->calculateSum($quantity);
89
        if ($sum === null) {
90
            return null;
91
        }
92
93
        $usage = $this->calculateUsage($quantity);
94
        if ($usage === null) {
95
            return null;
96
        }
97
98
        return $sum->divide(sprintf('%.14F', $usage->getQuantity()));
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104
    public function calculateUsage(QuantityInterface $quantity): ?QuantityInterface
105
    {
106
        return $quantity->convert($this->unit);
107
    }
108
}
109