Completed
Push — master ( 888950...2eb122 )
by Andrii
05:25
created

Plan::calculateCharges()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
ccs 6
cts 6
cp 1
cc 3
eloc 7
nc 3
nop 1
crap 3
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\charge\ChargeModifier;
17
use hiqdev\php\billing\customer\CustomerInterface;
18
use hiqdev\php\billing\price\PriceInterface;
19
20
/**
21
 * Tariff Plan.
22
 *
23
 * @author Andrii Vasyliev <[email protected]>
24
 */
25
class Plan implements PlanInterface
26
{
27
    /**
28
     * @var int
29
     */
30
    protected $id;
31
32
    /**
33
     * @var string
34
     */
35
    protected $name;
36
37
    /**
38
     * @var Plan|null
39
     * XXX not sure to implement
40
     */
41
    protected $parent;
42
43
    /**
44
     * @var CustomerInterface
45
     */
46
    protected $seller;
47
48
    /**
49
     * @var PriceInterface[]
50
     */
51
    protected $prices;
52
53
    /**
54
     * @param int $id
55
     * @param string $name
56
     * @param CustomerInterface|null $seller
57
     * @param PriceInterface[] $prices
58 1
     */
59
    public function __construct(
60
                            $id,
61
                            $name,
62
        CustomerInterface   $seller = null,
63
                            $prices = null
64 1
    ) {
65 1
        $this->id = $id;
66 1
        $this->name = $name;
67 1
        $this->seller = $seller;
68 1
        $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...
69
    }
70
71
    public function getUniqueId()
72
    {
73
        return $this->getId();
74
    }
75
76
    /**
77
     * @return int|string
78
     */
79
    public function getId()
80
    {
81
        return $this->id;
82
    }
83
84
    /**
85
     * @return bool
86 2
     */
87
    public function hasPrices()
88 2
    {
89
        return $this->prices !== null;
90
    }
91
92
    /**
93
     * @return PriceInterface[]
94
     */
95
    public function getPrices()
96
    {
97
        return $this->prices;
98
    }
99
100
    /**
101
     * @param PriceInterface[] $prices
102
     */
103
    public function setPrices(array $prices)
104
    {
105
        if ($this->hasPrices()) {
106
            throw new \Exception('cannot reassign prices for plan');
107
        }
108
        $this->prices = $prices;
109
    }
110
111
    /**
112
     * Calculate charges for given action.
113
     * @param ActionInterface $action
114
     * @return Charge[]|ChargeInterface[]
115 4
     */
116
    public function calculateCharges(ActionInterface $action)
117 4
    {
118 4
        $result = [];
119 4
        foreach ($this->prices as $price) {
120 4
            $charges = $price->calculateCharges($action);
121 4
            if ($charges) {
122
                $result = array_merge($result, $charges);
123
            }
124
        }
125 4
126
        return $result;
127
    }
128
129
    public function jsonSerialize()
130
    {
131
        return get_object_vars($this);
132
    }
133
134
    /**
135
     * @return CustomerInterface
136
     */
137
    public function getSeller(): ?CustomerInterface
138
    {
139
        return $this->seller;
140
    }
141
}
142