Passed
Pull Request — master (#75)
by
unknown
13:40
created

SinglePrice::getPrice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 1
cts 1
cp 1
crap 1
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-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;
16
use hiqdev\php\units\Quantity;
17
use hiqdev\php\units\QuantityInterface;
18
use Money\Money;
19
20
/**
21
 * Single Price.
22
 *
23
 * - no charge for quantity less then prepaid
24
 * - same price for any quantity above prepaid
25
 *
26
 * @see PriceInterface
27
 *
28
 * @author Andrii Vasyliev <[email protected]>
29
 */
30
class SinglePrice extends AbstractPrice implements PriceWithQuantityInterface,
31
    PriceWithMoneyInterface,
32
    PriceWithCurrencyInterface
33
{
34
    use HasMoney;
35
36
    /**
37
     * @var QuantityInterface prepaid quantity also implies Unit
38
     * XXX cannot be null cause Unit is required
39
     */
40
    protected $prepaid;
41
42 39
    public function __construct(
43
        $id,
44
        TypeInterface $type,
45
        TargetInterface $target,
46
        PlanInterface $plan = null,
47
        QuantityInterface $prepaid,
48
        Money $price
49
    ) {
50 39
        parent::__construct($id, $type, $target, $plan);
51 39
        $this->prepaid  = $prepaid;
52 39
        $this->price    = $price;
53 39
    }
54
55 3
    public function getPrepaid(): QuantityInterface
56
    {
57 3
        return $this->prepaid;
58
    }
59
60 3
    /**
61
     * {@inheritdoc}
62 3
     */
63
    public function calculateUsage(QuantityInterface $quantity): ?QuantityInterface
64
    {
65
        $usage = $quantity->convert($this->prepaid->getUnit())->subtract($this->prepaid);
66
67
        if ($usage->isPositive()) {
68 18
            return $usage;
69
        }
70 18
71
        return Quantity::create($this->prepaid->getUnit()->getName(), 0);
72 18
    }
73
74
    /**
75
     * {@inheritdoc}
76
     * Same price for any usage.
77
     */
78
    public function calculatePrice(QuantityInterface $usage): ?Money
79 14
    {
80
        return $this->price;
81 14
    }
82
}
83