Completed
Push — master ( efd5b5...1cc014 )
by Andrii
02:16
created

Plan::calculateCharges()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5.5069

Importance

Changes 0
Metric Value
dl 0
loc 17
ccs 8
cts 11
cp 0.7272
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 11
nc 5
nop 1
crap 5.5069
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
     */
59 1
    public function __construct(
60
                            $id,
61
                            $name,
62
        CustomerInterface   $seller = null,
63
                            $prices = null
64
    ) {
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 1
    }
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
     */
87 2
    public function hasPrices()
88
    {
89 2
        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
     */
116 4
    public function calculateCharges(ActionInterface $action)
117
    {
118 4
        $result = [];
119 4
        foreach ($this->prices as $price) {
120 4
            $charge = $action->calculateCharge($price);
121 4
            if ($price instanceof ChargeModifier) {
122
                $charges = $price->modifyCharge($charge, $action);
123
                if ($charges) {
124
                    $result = array_merge($result, $charges);
125
                }
126 4
            } elseif ($charge !== null) {
127 4
                $result[] = $charge;
128
            }
129
        }
130
131 4
        return $result;
132
    }
133
134
    public function jsonSerialize()
135
    {
136
        return get_object_vars($this);
137
    }
138
139
    /**
140
     * @return CustomerInterface
141
     */
142
    public function getSeller(): ?CustomerInterface
143
    {
144
        return $this->seller;
145
    }
146
}
147