Passed
Push — master ( 15d4af...5593ee )
by Andrii
02:35
created

Bill::getUniqueString()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 4
nop 0
dl 0
loc 12
ccs 8
cts 8
cp 1
crap 3
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
    /** @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 1
    public function getUniqueString(): string
87
    {
88
        $parts = [
89 1
            'buyer'     => $this->customer->getUniqueId(),
90 1
            'currency'  => $this->sum->getCurrency()->getCode(),
91 1
            'target'    => $this->target ? $this->target->getUniqueId() : null,
92 1
            'type'      => $this->type->getUniqueId(),
93 1
            'time'      => $this->time->format('c'),
94 1
            'plan'      => $this->plan ? $this->plan->getUniqueId() : null,
95
        ];
96
97 1
        return implode('-', $parts);
98
    }
99
100
    public function calculatePrice()
101
    {
102
        $quantity = $this->quantity->getQuantity();
103
104
        return $quantity ? $this->sum->divide($quantity) : $this->sum;
105
    }
106
107
    /**
108
     * @return int|string
109
     */
110
    public function getId()
111
    {
112
        return $this->id;
113
    }
114
115
    public function setId($id)
116
    {
117
        if ($this->id === $id) {
118
            return;
119
        }
120
        if ($this->id !== null) {
121
            throw new \Exception('cannot reassign bill id');
122
        }
123
        $this->id = $id;
124
    }
125
126
    /**
127
     * @return TypeInterface
128
     */
129 1
    public function getType(): TypeInterface
130
    {
131 1
        return $this->type;
132
    }
133
134
    /**
135
     * @return DateTimeImmutable
136
     */
137 1
    public function getTime(): DateTimeImmutable
138
    {
139 1
        return $this->time;
140
    }
141
142
    /**
143
     * @return TargetInterface
144
     */
145 1
    public function getTarget()
146
    {
147 1
        return $this->target;
148
    }
149
150
    /**
151
     * @return CustomerInterface
152
     */
153 1
    public function getCustomer()
154
    {
155 1
        return $this->customer;
156
    }
157
158
    /**
159
     * @return QuantityInterface
160
     */
161 1
    public function getQuantity()
162
    {
163 1
        return $this->quantity;
164
    }
165
166
    /**
167
     * @return Money
168
     */
169 1
    public function getSum()
170
    {
171 1
        return $this->sum;
172
    }
173
174
    /**
175
     * @return PlanInterface
176
     */
177 1
    public function getPlan()
178
    {
179 1
        return $this->plan;
180
    }
181
182
    /**
183
     * @return ChargeInterface[]
184
     */
185 1
    public function getCharges()
186
    {
187 1
        return $this->charges;
188
    }
189
190
    public function getState(): ?BillState
191
    {
192
        return $this->state;
193
    }
194
195
    public function setFinished(): void
196
    {
197
        $this->state = BillState::finished();
198
    }
199
200
    public function isFinished(): ?bool
201
    {
202
        return $this->state === null ? null : $this->state->isFinished();
203
    }
204
205
    public function getComment()
206
    {
207
        return $this->comment;
208
    }
209
210
    public function setComment(string $comment)
211
    {
212
        $this->comment = $comment;
213
    }
214
215
    public function jsonSerialize()
216
    {
217
        return get_object_vars($this);
218
    }
219
}
220