Completed
Push — master ( 6ed56d...9a6247 )
by Andrii
03:57
created

Bill::getId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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
    /** @var int|string */
30
    protected $id;
31
32
    /** @var TypeInterface */
33
    protected $type;
34
35
    /** @var DateTimeImmutable */
36
    protected $time;
37
38
    /** @var Money */
39
    protected $sum;
40
41
    /** @var QuantityInterface */
42
    protected $quantity;
43
44
    /** @var CustomerInterface */
45
    protected $customer;
46
47
    /** @var TargetInterface */
48
    protected $target;
49
50
    /** @var PlanInterface */
51
    protected $plan;
52
53
    /** @var ChargeInterface[] */
54
    protected $charges = [];
55
56
    /** @var BillState */
57
    protected $state;
58
59
    /** @var string */
60
    protected $comment;
61
62 1
    public function __construct(
63
                            $id,
64
        TypeInterface       $type,
65
        DateTimeImmutable   $time,
66
        Money               $sum,
67
        QuantityInterface   $quantity,
68
        CustomerInterface   $customer,
69
        TargetInterface     $target = null,
70
        PlanInterface       $plan = null,
71
        array               $charges = [],
72
        BillState           $state = null
73
    ) {
74 1
        $this->id           = $id;
75 1
        $this->type         = $type;
76 1
        $this->time         = $time;
77 1
        $this->sum          = $sum;
78 1
        $this->quantity     = $quantity;
79 1
        $this->customer     = $customer;
80 1
        $this->target       = $target;
81 1
        $this->plan         = $plan;
82 1
        $this->charges      = $charges;
83 1
        $this->state        = $state;
84 1
    }
85
86
    /**
87
     * Provides unique string.
88
     * Can be used to compare or aggregate bills.
89
     */
90 1
    public function getUniqueString(): string
91
    {
92
        $parts = [
93 1
            'currency'  => $this->sum->getCurrency()->getCode(),
94 1
            'buyer'     => $this->customer->getUniqueId(),
95 1
            'target'    => $this->target ? $this->target->getUniqueId() : null,
96 1
            'type'      => $this->type->getUniqueId(),
97 1
            'time'      => $this->time->format('c'),
98 1
            'plan'      => $this->plan ? $this->plan->getUniqueId() : null,
99
        ];
100
101 1
        return implode('-', $parts);
102
    }
103
104
    public function calculatePrice()
105
    {
106
        $quantity = $this->quantity->getQuantity();
107
108
        return $quantity ? $this->sum->divide($quantity) : $this->sum;
109
    }
110
111
    /**
112
     * @return int|string
113
     */
114 1
    public function getId()
115
    {
116 1
        return $this->id;
117
    }
118
119
    public function setId($id)
120
    {
121
        if ($this->id === $id) {
122
            return;
123
        }
124
        if ($this->id !== null) {
125
            throw new \Exception('cannot reassign bill id');
126
        }
127
        $this->id = $id;
128
    }
129
130
    /**
131
     * @return TypeInterface
132
     */
133 1
    public function getType(): TypeInterface
134
    {
135 1
        return $this->type;
136
    }
137
138
    /**
139
     * @return DateTimeImmutable
140
     */
141 1
    public function getTime(): DateTimeImmutable
142
    {
143 1
        return $this->time;
144
    }
145
146
    /**
147
     * @return TargetInterface
148
     */
149 1
    public function getTarget()
150
    {
151 1
        return $this->target;
152
    }
153
154
    /**
155
     * @return CustomerInterface
156
     */
157 1
    public function getCustomer()
158
    {
159 1
        return $this->customer;
160
    }
161
162
    /**
163
     * @return QuantityInterface
164
     */
165 1
    public function getQuantity()
166
    {
167 1
        return $this->quantity;
168
    }
169
170
    /**
171
     * @return BillInterface
172
     */
173
    public function setQuantity(QuantityInterface $quantity): BillInterface
174
    {
175
        $this->quantity = $quantity;
176
177
        return $this;
178
    }
179
180
    /**
181
     * @return Money
182
     */
183 1
    public function getSum()
184
    {
185 1
        return $this->sum;
186
    }
187
188
    /**
189
     * @return PlanInterface
190
     */
191 1
    public function getPlan()
192
    {
193 1
        return $this->plan;
194
    }
195
196
    /**
197
     * @return bool
198
     */
199
    public function hasCharges(): bool
200
    {
201
        return $this->charges !== [];
202
    }
203
204
    /**
205
     * @return ChargeInterface[]
206
     */
207 1
    public function getCharges(): array
208
    {
209 1
        return $this->charges;
210
    }
211
212
    /**
213
     * @param ChargeInterface[] $prices
214
     * @return self
215
     * @throws \Exception
216
     */
217
    public function setCharges(array $charges): self
218
    {
219
        if ($this->hasCharges()) {
220
            throw new \Exception('cannot reassign charges for bill');
221
        }
222
        $this->charges = $charges;
223
224
        return $this;
225
    }
226
227
    public function getState(): ?BillState
228
    {
229
        return $this->state;
230
    }
231
232
    public function setFinished(): void
233
    {
234
        $this->state = BillState::finished();
235
    }
236
237
    public function isFinished(): ?bool
238
    {
239
        return $this->state === null ? null : $this->state->isFinished();
240
    }
241
242
    public function getComment()
243
    {
244
        return $this->comment;
245
    }
246
247
    public function setComment(string $comment)
248
    {
249
        $this->comment = $comment;
250
    }
251
252
    public function jsonSerialize()
253
    {
254
        return get_object_vars($this);
255
    }
256
}
257