Completed
Push — master ( 89e825...a5de38 )
by Andrii
02:03
created

EnumPrice::getPrices()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hiqdev\php\billing\price;
12
13
use hiqdev\php\billing\target\TargetInterface;
14
use hiqdev\php\billing\type\TypeInterface;
15
use hiqdev\php\units\QuantityInterface;
16
use hiqdev\php\units\UnitInterface;
17
use Money\Currency;
18
use Money\Money;
19
20
/**
21
 * Enum Price:
22
 * - holds sums list: amount => total sum for the quantity NOT price per unit
23
 * - listed quantities only else exception.
24
 * @see PriceInterface
25
 *
26
 * @author Andrii Vasyliev <[email protected]>
27
 */
28
class EnumPrice extends AbstractPrice
29
{
30
    /**
31
     * @var UnitInterface
32
     */
33
    protected $unit;
34
35
    /**
36
     * @var Currency
37
     */
38
    protected $currency;
39
40
    /**
41
     * @var array quantity => total sum for the quantity
42
     */
43
    protected $sums;
44
45 2
    public function __construct(
46
                            $id,
47
        TypeInterface       $type,
48
        TargetInterface     $target,
49
        UnitInterface       $unit,
50
        Currency            $currency,
51
        array               $sums
52
    ) {
53 2
        parent::__construct($id, $type, $target);
54 2
        $this->unit = $unit;
55 2
        $this->currency = $currency;
56 2
        $this->sums = $sums;
57 2
    }
58
59 1
    public function getUnit()
60
    {
61 1
        return $this->unit;
62
    }
63
64 1
    public function getCurrency()
65
    {
66 1
        return $this->currency;
67
    }
68
69 1
    public function getSums()
70
    {
71 1
        return $this->sums;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 2
    public function calculateSum(QuantityInterface $quantity)
78
    {
79 2
        $usage = $this->calculateUsage($quantity)->getQuantity();
80
81 2
        foreach ($this->sums as $value => $price) {
82 2
            if ((string) $value === (string) $usage) {
83 2
                return new Money($price, $this->currency);
84
            }
85
        }
86
87
        throw new FailedCalculatePriceException('not enumed quantity: ' . $usage);
88
    }
89
90
    /**
91
     * {@inheritdoc}
92
     */
93
    public function calculatePrice(QuantityInterface $quantity)
94
    {
95
        $sum = $this->calculateSum($quantity);
96
        if ($sum === null) {
97
            return null;
98
        }
99
100
        $usage = $this->calculateUsage($quantity);
101
        if ($usage === null) {
102
            return null;
103
        }
104
105
        return $sum->divide($usage->getQuantity());
106
    }
107
108
    /**
109
     * {@inheritdoc}
110
     */
111 2
    public function calculateUsage(QuantityInterface $quantity)
112
    {
113 2
        return $quantity->convert($this->unit);
114
    }
115
}
116