Completed
Push — master ( 2ef50e...18ed3e )
by Dmitry
02:37
created

AbstractPrice::calculatePrice()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
ccs 0
cts 0
cp 0
c 0
b 0
f 0
nc 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;
12
13
use hiqdev\php\units\QuantityInterface;
14
15
/**
16
 * Price.
17
 * @see PriceInterface
18
 *
19
 * @author Andrii Vasyliev <[email protected]>
20
 */
21
abstract class AbstractPrice implements PriceInterface, EntityInterface
22
{
23
    /**
24
     * @var integer
25
     */
26
    protected $id;
27
28
    /**
29
     * @var Target
30
     */
31
    protected $target;
32
33
    /**
34
     * @var Type
35
     */
36
    protected $type;
37
38 3
    public function __construct($id, TargetInterface $target, TypeInterface $type)
39
    {
40 3
        $this->id = $id;
41 3
        $this->target = $target;
0 ignored issues
show
Documentation Bug introduced by
$target is of type object<hiqdev\php\billing\TargetInterface>, but the property $target was declared to be of type object<hiqdev\php\billing\Target>. 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...
42 3
        $this->type = $type;
0 ignored issues
show
Documentation Bug introduced by
$type is of type object<hiqdev\php\billing\TypeInterface>, but the property $type was declared to be of type object<hiqdev\php\billing\Type>. 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...
43 3
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 2
    public function getTarget()
49
    {
50 2
        return $this->target;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 2
    public function getType()
57
    {
58 2
        return $this->type;
59
    }
60
61
    /**
62
     * {@inheritdoc}
63
     * Default sum calculation method: sum = price * usage.
64
     */
65 1
    public function calculateSum(QuantityInterface $quantity)
66
    {
67 1
        $usage = $this->calculateUsage($quantity);
68 1
        if ($usage === null) {
69
            return null;
70
        }
71
72 1
        $price = $this->calculatePrice($quantity);
73 1
        if ($price === null) {
74
            return null;
75
        }
76
77 1
        return $price->multiply($usage->getQuantity());
78
    }
79
80
    public static function create(array $data)
81
    {
82
        return new SinglePrice($data['id'], $data['target']);
0 ignored issues
show
Bug introduced by
The call to SinglePrice::__construct() misses some required arguments starting with $type.
Loading history...
83
    }
84
85
    public function jsonSerialize()
86
    {
87
        return [
88
            'id' => $this->id,
89
            'type' => $this->type,
90
            'target' => $this->target,
91
        ];
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    abstract public function calculateUsage(QuantityInterface $quantity);
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    abstract public function calculatePrice(QuantityInterface $action);
103
}
104