Passed
Push — master ( 373e60...256112 )
by Dmitry
25:12 queued 10:02
created

SinglePrice::calculatePrice()   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 1
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;
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\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, PriceWithMoneyInterface
31
{
32
    use HasMoney;
33
    use HasQuantity;
34
35
    public function __construct(
36
        $id,
37
        TypeInterface $type,
38
        TargetInterface $target,
39
        PlanInterface $plan = null,
40
        QuantityInterface $prepaid,
41
        Money $price
42 39
    ) {
43
        parent::__construct($id, $type, $target, $plan);
44
        $this->prepaid  = $prepaid;
45
        $this->price    = $price;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50 39
     */
51 39
    public function calculateUsage(QuantityInterface $quantity): ?QuantityInterface
52 39
    {
53 39
        $usage = $quantity->convert($this->prepaid->getUnit())->subtract($this->prepaid);
54
55 3
        if ($usage->isPositive()) {
56
            return $usage;
57 3
        }
58
59
        return Quantity::create($this->prepaid->getUnit()->getName(), 0);
60 3
    }
61
62 3
    /**
63
     * {@inheritdoc}
64
     * Same price for any usage.
65
     */
66
    public function calculatePrice(QuantityInterface $usage): ?Money
67
    {
68 18
        return $this->price;
69
    }
70
}
71