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

SinglePrice::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 2
rs 10
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\charge\ChargeModifier;
14
use hiqdev\php\billing\charge\FormulaChargeModifierTrait;
0 ignored issues
show
Bug introduced by
The type hiqdev\php\billing\charg...mulaChargeModifierTrait 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...
15
use hiqdev\php\billing\plan\PlanInterface;
16
use hiqdev\php\billing\target\TargetInterface;
17
use hiqdev\php\billing\type\TypeInterface;
18
use hiqdev\php\units\Quantity;
19
use hiqdev\php\units\QuantityInterface;
20
use Money\Money;
21
22
/**
23
 * Single Price.
24
 *
25
 * - no charge for quantity less then prepaid
26
 * - same price for any quantity above prepaid
27
 *
28
 * TODO add `$modifier` property instead of FormulaChargeModifierTrait
29
 *
30
 * @see PriceInterface
31
 *
32
 * @author Andrii Vasyliev <[email protected]>
33
 */
34
class SinglePrice extends AbstractPrice
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
    /**
43
     * @var Money
44
     */
45 32
    protected $price;
46
47
    public function __construct(
48
                            $id,
49
        TypeInterface       $type,
50
        TargetInterface     $target,
51
        PlanInterface       $plan = null,
52
        QuantityInterface   $prepaid,
53 32
        Money               $price
54 32
    ) {
55 32
        parent::__construct($id, $type, $target, $plan);
56 32
        $this->prepaid  = $prepaid;
57
        $this->price    = $price;
58 1
    }
59
60 1
    public function getPrepaid()
61
    {
62
        return $this->prepaid;
63 1
    }
64
65 1
    public function getPrice()
66
    {
67
        return $this->price;
68
    }
69
70
    /**
71 18
     * {@inheritdoc}
72
     */
73 18
    public function calculateUsage(QuantityInterface $quantity): ?QuantityInterface
74
    {
75 18
        $usage = $quantity->convert($this->prepaid->getUnit())->subtract($this->prepaid);
76
77
        return $usage->isPositive() ? $usage : null;
78
    }
79
80
    /**
81
     * {@inheritdoc}
82 14
     * Same price for any usage.
83
     */
84 14
    public function calculatePrice(QuantityInterface $usage): ?Money
85
    {
86
        return $this->price;
87
    }
88
89
}
90