Completed
Push — master ( 725d9d...16077c )
by Dmitry
05:29
created

Plan::getSeller()   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 int $id
54
     * @param string $name
55
     * @param CustomerInterface|null $seller
56
     * @param PriceInterface[] $prices
57
     */
58 1
    public function __construct(
59
                            $id,
60
                            $name,
61
        CustomerInterface   $seller = null,
62
                            $prices = null
63
    ) {
64 1
        $this->id = $id;
65 1
        $this->name = $name;
66 1
        $this->seller = $seller;
67 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...
68 1
    }
69
70
    public function getUniqueId()
71
    {
72
        return $this->getId();
73
    }
74
75
    /**
76
     * @return int|string
77
     */
78
    public function getId()
79
    {
80
        return $this->id;
81
    }
82
83
    /**
84
     * @return bool
85
     */
86 2
    public function hasPrices()
87
    {
88 2
        return $this->prices !== null;
89
    }
90
91
    /**
92
     * @return PriceInterface[]
93
     */
94
    public function getPrices()
95
    {
96
        return $this->prices;
97
    }
98
99
    /**
100
     * @param PriceInterface[] $prices
101
     */
102
    public function setPrices(array $prices)
103
    {
104
        if ($this->hasPrices()) {
105
            throw new \Exception('cannot reassign prices for plan');
106
        }
107
        $this->prices = $prices;
108
    }
109
110
    /**
111
     * Calculate charges for given action.
112
     * @param ActionInterface $action
113
     * @return Charge[]|ChargeInterface[]
114
     */
115 4
    public function calculateCharges(ActionInterface $action)
116
    {
117 4
        $charges = [];
118 4
        foreach ($this->prices as $price) {
119 4
            $charge = $action->calculateCharge($price);
120 4
            if ($charge !== null) {
121 4
                $charges[] = $charge;
122
            }
123
        }
124
125 4
        return $charges;
126
    }
127
128
    public function jsonSerialize()
129
    {
130
        return get_object_vars($this);
131
    }
132
133
    /**
134
     * @return CustomerInterface
135
     */
136
    public function getSeller(): ?CustomerInterface
137
    {
138
        return $this->seller;
139
    }
140
}
141