Completed
Push — master ( a4f70a...5b7a0f )
by Andrii
05:16
created

Plan::hasPrices()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
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\plan;
12
13
use hiqdev\php\billing\action\ActionInterface;
14
use hiqdev\php\billing\charge\Charge;
15
use hiqdev\php\billing\charge\ChargeInterface;
16
use hiqdev\php\billing\customer\CustomerInterface;
17
use hiqdev\php\billing\price\PriceInterface;
18
19
/**
20
 * Tariff Plan.
21
 *
22
 * @author Andrii Vasyliev <[email protected]>
23
 */
24
class Plan implements PlanInterface
25
{
26
    /**
27
     * @var int
28
     */
29
    protected $id;
30
31
    /**
32
     * @var string
33
     */
34
    protected $name;
35
36
    /**
37
     * @var Plan|null
38
     * XXX not sure to implement
39
     */
40
    protected $parent;
41
42
    /**
43
     * @var CustomerInterface
44
     */
45
    protected $seller;
46
47
    /**
48
     * @var PriceInterface[]
49
     */
50
    protected $prices = [];
51
52
    /**
53
     * @param PriceInterface[] $prices
54
     */
55
    public function __construct(
56
                            $id,
57
                            $name,
58
        CustomerInterface   $seller = null,
59
                            $prices = null
60
    ) {
61
        $this->id = $id;
62
        $this->name = $name;
63
        $this->seller = $seller;
64
        $this->prices = $prices;
0 ignored issues
show
Documentation Bug introduced by
It seems like $prices can be null. However, the property $prices is declared as array. Maybe change the type of the property to array|null or add a type check?

Our type inference engine has found an assignment of a scalar value (like a string, an integer or null) to a property which is an array.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property.

To type hint that a parameter can be either an array or null, you can set a type hint of array and a default value of null. The PHP interpreter will then accept both an array or null for that parameter.

function aContainsB(array $needle = null, array  $haystack) {
    if (!$needle) {
        return false;
    }

    return array_intersect($haystack, $needle) == $haystack;
}

The function can be called with either null or an array for the parameter $needle but will only accept an array as $haystack.

Loading history...
65
    }
66
67
    public function getUniqueId()
68
    {
69
        return $this->getId();
70
    }
71
72
    /**
73
     * @return int|string
74
     */
75
    public function getId()
76
    {
77
        return $this->id;
78
    }
79
80
    /**
81
     * @return bool
82
     */
83
    public function hasPrices()
84
    {
85
        return $this->prices !== null;
86
    }
87
88
    /**
89
     * @return PriceInterface[]
90
     */
91
    public function getPrices()
92
    {
93
        return $this->prices;
94
    }
95
96
    /**
97
     * @param PriceInterface[] $prices
98
     */
99
    public function setPrices(array $prices)
100
    {
101
        if ($this->hasPrices()) {
102
            throw new \Exception('cannot reassign prices for plan');
103
        }
104
        $this->prices = $prices;
105
    }
106
107
    /**
108
     * Calculate charges for given action.
109
     * @param ActionInterface $action
110
     * @return Charge[]|ChargeInterface[]
111
     */
112 1
    public function calculateCharges(ActionInterface $action)
113
    {
114 1
        $charges = [];
115 1
        foreach ($this->prices as $price) {
116 1
            $charge = $action->calculateCharge($price);
117 1
            if ($charge !== null) {
118 1
                $charges[] = $charge;
119
            }
120
        }
121
122 1
        return $charges;
123
    }
124
125
    public function jsonSerialize()
126
    {
127
        return get_object_vars($this);
128
    }
129
}
130