Completed
Push — master ( a4f70a...5b7a0f )
by Andrii
05:16
created

Bill::jsonSerialize()   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
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
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\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
    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
        $this->id           = $id;
92
        $this->type         = $type;
93
        $this->time         = $time;
94
        $this->sum          = $sum;
95
        $this->quantity     = $quantity;
96
        $this->customer     = $customer;
97
        $this->target       = $target;
98
        $this->plan         = $plan;
99
        $this->charges      = $charges;
100
        $this->isFinished   = $isFinished;
101
    }
102
103
    public function getUniqueString()
104
    {
105
        $parts = [
106
            'buyer'     => $this->customer->getUniqueId(),
107
            'currency'  => $this->sum->getCurrency()->getCode(),
108
            'target'    => $this->target ? $this->target->getUniqueId() : null,
109
            'type'      => $this->type->getUniqueId(),
110
            'time'      => $this->time->format('c'),
111
            'plan'      => $this->plan ? $this->plan->getUniqueId() : null,
112
        ];
113
114
        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 View Code Duplication
    public function setId($id)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
    public function getType()
147
    {
148
        return $this->type;
149
    }
150
151
    /**
152
     * @return DateTimeImmutable
153
     */
154
    public function getTime()
155
    {
156
        return $this->time;
157
    }
158
159
    /**
160
     * @return TargetInterface
161
     */
162
    public function getTarget()
163
    {
164
        return $this->target;
165
    }
166
167
    /**
168
     * @return CustomerInterface
169
     */
170
    public function getCustomer()
171
    {
172
        return $this->customer;
173
    }
174
175
    /**
176
     * @return QuantityInterface
177
     */
178
    public function getQuantity()
179
    {
180
        return $this->quantity;
181
    }
182
183
    /**
184
     * @return Money
185
     */
186
    public function getSum()
187
    {
188
        return $this->sum;
189
    }
190
191
    /**
192
     * @return PlanInterface
193
     */
194
    public function getPlan()
195
    {
196
        return $this->plan;
197
    }
198
199
    /**
200
     * @return ChargeInterface[]
201
     */
202
    public function getCharges()
203
    {
204
        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