Completed
Push — master ( d26128...b08c4e )
by Andrii
03:02
created

Bill::calculatePrice()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 6
rs 9.4285
c 0
b 0
f 0
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\bill;
12
13
use DateTimeImmutable;
14
use hiqdev\php\billing\charge\ChargeInterface;
15
use hiqdev\php\billing\customer\CustomerInterface;
16
use hiqdev\php\billing\plan\PlanInterface;
17
use hiqdev\php\billing\target\TargetInterface;
18
use hiqdev\php\billing\type\TypeInterface;
19
use hiqdev\php\units\QuantityInterface;
20
use Money\Money;
21
22
/**
23
 * Bill.
24
 *
25
 * @author Andrii Vasyliev <[email protected]>
26
 */
27
class Bill implements BillInterface
28
{
29
    /**
30
     * @var int|string
31
     */
32
    protected $id;
33
34
    /**
35
     * @var TypeInterface
36
     */
37
    protected $type;
38
39
    /**
40
     * @var DateTimeImmutable
41
     */
42
    protected $time;
43
44
    /**
45
     * @var Money
46
     */
47
    protected $sum;
48
49
    /**
50
     * @var QuantityInterface
51
     */
52
    protected $quantity;
53
54
    /**
55
     * @var CustomerInterface
56
     */
57
    protected $customer;
58
59
    /**
60
     * @var TargetInterface
61
     */
62
    protected $target;
63
64
    /**
65
     * @var PlanInterface
66
     */
67
    protected $plan;
68
69
    /**
70
     * @var bool
71
     */
72
    protected $isFinished = false;
73
74
    /**
75
     * @var ChargeInterface[]
76
     */
77
    protected $charges = [];
78
79 1
    public function __construct(
80
                            $id,
81
        TypeInterface       $type,
82
        DateTimeImmutable   $time,
83
        Money               $sum,
84
        QuantityInterface   $quantity,
85
        CustomerInterface   $customer,
86
        TargetInterface     $target = null,
87
        PlanInterface       $plan = null,
88
        array               $charges = [],
89
        bool                $isFinished = false
90
    ) {
91 1
        $this->id           = $id;
92 1
        $this->type         = $type;
93 1
        $this->time         = $time;
94 1
        $this->sum          = $sum;
95 1
        $this->quantity     = $quantity;
96 1
        $this->customer     = $customer;
97 1
        $this->target       = $target;
98 1
        $this->plan         = $plan;
99 1
        $this->charges      = $charges;
100 1
        $this->isFinished   = $isFinished;
101 1
    }
102
103 1
    public function getUniqueString()
104
    {
105
        $parts = [
106 1
            'buyer'     => $this->customer->getUniqueId(),
107 1
            'currency'  => $this->sum->getCurrency()->getCode(),
108 1
            'target'    => $this->target ? $this->target->getUniqueId() : null,
109 1
            'type'      => $this->type->getUniqueId(),
0 ignored issues
show
Bug introduced by
The method getUniqueId() does not exist on hiqdev\php\billing\type\TypeInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to hiqdev\php\billing\type\TypeInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

109
            'type'      => $this->type->/** @scrutinizer ignore-call */ getUniqueId(),
Loading history...
110 1
            'time'      => $this->time->format('c'),
111 1
            'plan'      => $this->plan ? $this->plan->getUniqueId() : null,
0 ignored issues
show
Bug introduced by
The method getUniqueId() does not exist on hiqdev\php\billing\plan\PlanInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to hiqdev\php\billing\plan\PlanInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

111
            'plan'      => $this->plan ? $this->plan->/** @scrutinizer ignore-call */ getUniqueId() : null,
Loading history...
112
        ];
113
114 1
        return implode('-', $parts);
115
    }
116
117
    public function calculatePrice()
118
    {
119
        $quantity = $this->quantity->getQuantity();
120
121
        return $quantity ? $this->sum->divide($quantity) : $this->sum;
122
    }
123
124
    /**
125
     * @return int|string
126
     */
127
    public function getId()
128
    {
129
        return $this->id;
130
    }
131
132
    public function setId($id)
133
    {
134
        if ($this->id === $id) {
135
            return;
136
        }
137
        if ($this->id !== null) {
138
            throw new \Exception('cannot reassign bill id');
139
        }
140
        $this->id = $id;
141
    }
142
143
    /**
144
     * @return TypeInterface
145
     */
146 1
    public function getType()
147
    {
148 1
        return $this->type;
149
    }
150
151
    /**
152
     * @return DateTimeImmutable
153
     */
154 1
    public function getTime()
155
    {
156 1
        return $this->time;
157
    }
158
159
    /**
160
     * @return TargetInterface
161
     */
162 1
    public function getTarget()
163
    {
164 1
        return $this->target;
165
    }
166
167
    /**
168
     * @return CustomerInterface
169
     */
170 1
    public function getCustomer()
171
    {
172 1
        return $this->customer;
173
    }
174
175
    /**
176
     * @return QuantityInterface
177
     */
178 1
    public function getQuantity()
179
    {
180 1
        return $this->quantity;
181
    }
182
183
    /**
184
     * @return Money
185
     */
186 1
    public function getSum()
187
    {
188 1
        return $this->sum;
189
    }
190
191
    /**
192
     * @return PlanInterface
193
     */
194 1
    public function getPlan()
195
    {
196 1
        return $this->plan;
197
    }
198
199
    /**
200
     * @return ChargeInterface[]
201
     */
202 1
    public function getCharges()
203
    {
204 1
        return $this->charges;
205
    }
206
207
    /**
208
     * @return bool
209
     */
210
    public function getIsFinished()
211
    {
212
        return $this->isFinished;
213
    }
214
215
    public function jsonSerialize()
216
    {
217
        return get_object_vars($this);
218
    }
219
}
220