Passed
Push — master ( bb3e2c...438d9c )
by Andrii
03:09
created

EnumPrice::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 0
dl 0
loc 6
ccs 0
cts 5
cp 0
crap 2
rs 10
c 0
b 0
f 0
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-2018, 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;
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
30
{
31
    /**
32
     * @var UnitInterface
33
     */
34
    protected $unit;
35
36
    /**
37
     * @var Currency
38
     */
39
    protected $currency;
40
41
    /**
42
     * @var array quantity => total sum for the quantity
43
     */
44
    protected $sums;
45
46 2
    public function __construct(
47
                            $id,
48
        TypeInterface       $type,
49
        TargetInterface     $target,
50
        ?PlanInterface      $plan,
51
        UnitInterface       $unit,
52
        Currency            $currency,
53
        array               $sums
54
    ) {
55 2
        parent::__construct($id, $type, $target, $plan);
56 2
        $this->unit = $unit;
57 2
        $this->currency = $currency;
58 2
        $this->sums = $sums;
59 2
    }
60
61 1
    public function getUnit()
62
    {
63 1
        return $this->unit;
64
    }
65
66 1
    public function getCurrency()
67
    {
68 1
        return $this->currency;
69
    }
70
71 1
    public function getSums()
72
    {
73 1
        return $this->sums;
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 4
    public function calculateSum(QuantityInterface $quantity): ?Money
80
    {
81 4
        $usage = $this->calculateUsage($quantity)->getQuantity();
82
83 4
        foreach ($this->sums as $value => $price) {
84 4
            if ((string) $value === (string) $usage) {
85 4
                return new Money($price, $this->currency);
86
            }
87
        }
88
89
        throw new FailedCalculatePriceException('not enumed quantity: ' . $usage);
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95
    public function calculatePrice(QuantityInterface $quantity): ?Money
96
    {
97
        $sum = $this->calculateSum($quantity);
98
        if ($sum === null) {
99
            return null;
100
        }
101
102
        $usage = $this->calculateUsage($quantity);
103
        if ($usage === null) {
104
            return null;
105
        }
106
107
        return $sum->divide($usage->getQuantity());
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113 4
    public function calculateUsage(QuantityInterface $quantity): ?QuantityInterface
114
    {
115 4
        return $quantity->convert($this->unit);
116
    }
117
118
}
119