Completed
Push — master ( b28d3c...039260 )
by Dmitry
02:38
created

SinglePrice::jsonSerialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 7
ccs 0
cts 4
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
crap 2
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\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
31
{
32
    /**
33
     * @var Quantity prepaid quantity also implies Unit
34
     */
35
    protected $prepaid;
36
37
    /**
38
     * @var Money
39
     */
40
    protected $price;
41
42 3
    public function __construct(
43
                            $id,
44
        TypeInterface       $type,
45
        TargetInterface     $target,
46
        PlanInterface       $plan = null,
47
        QuantityInterface   $prepaid,
48
        Money               $price
49
    ) {
50 3
        parent::__construct($id, $type, $target, $plan);
51 3
        $this->prepaid  = $prepaid;
0 ignored issues
show
Documentation Bug introduced by
$prepaid is of type object<hiqdev\php\units\QuantityInterface>, but the property $prepaid was declared to be of type object<hiqdev\php\units\Quantity>. Are you sure that you always receive this specific sub-class here, or does it make sense to add an instanceof check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.

Either this assignment is in error or an instanceof check should be added for that assignment.

class Alien {}

class Dalek extends Alien {}

class Plot
{
    /** @var  Dalek */
    public $villain;
}

$alien = new Alien();
$plot = new Plot();
if ($alien instanceof Dalek) {
    $plot->villain = $alien;
}
Loading history...
52 3
        $this->price    = $price;
53 3
    }
54
55 1
    public function getPrepaid()
56
    {
57 1
        return $this->prepaid;
58
    }
59
60 1
    public function getPrice()
61
    {
62 1
        return $this->price;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68 1
    public function calculateUsage(QuantityInterface $quantity): ?QuantityInterface
69
    {
70 1
        $usage = $quantity->convert($this->prepaid->getUnit())->subtract($this->prepaid);
71
72 1
        return $usage->isPositive() ? $usage : null;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     * Same price for any usage.
78
     */
79 1
    public function calculatePrice(QuantityInterface $usage): ?Money
80
    {
81 1
        return $this->price;
82
    }
83
84
    public function jsonSerialize()
85
    {
86
        return array_merge(parent::jsonSerialize(), [
87
            'prepaid' => $this->prepaid,
88
            'price' => $this->price
89
        ]);
90
    }
91
}
92