Completed
Push — master ( 950b87...b5a5d9 )
by Andrii
03:47
created

Bill   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 172
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 83.33%

Importance

Changes 0
Metric Value
wmc 16
lcom 1
cbo 5
dl 0
loc 172
ccs 35
cts 42
cp 0.8333
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 21 1
A calculatePrice() 0 6 2
A getId() 0 4 1
A getType() 0 4 1
A getTime() 0 4 1
A getTarget() 0 4 1
A getCustomer() 0 4 1
A getQuantity() 0 4 1
A getSum() 0 4 1
A getPlan() 0 4 1
A getCharges() 0 4 1
A jsonSerialize() 0 4 1
A getUniqueString() 0 13 3
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, 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;
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
    ) {
90 1
        $this->id       = $id;
91 1
        $this->type     = $type;
92 1
        $this->time     = $time;
93 1
        $this->sum      = $sum;
94 1
        $this->quantity = $quantity;
95 1
        $this->customer = $customer;
96 1
        $this->target   = $target;
97 1
        $this->plan     = $plan;
98 1
        $this->charges  = $charges;
99 1
    }
100
101 1
    public function getUniqueString()
102
    {
103
        $parts = [
104 1
            $this->customer->getUniqueId(),
105 1
            $this->sum->getCurrency()->getCode(),
106 1
            $this->target ? $this->target->getUniqueId() : null,
107 1
            $this->type->getUniqueId(),
108 1
            $this->time->format('c'),
109 1
            $this->plan ? $this->plan->getUniqueId() : null,
110
        ];
111
112 1
        return implode('-', array_filter($parts));
113
    }
114
115
    public function calculatePrice()
116
    {
117
        $quantity = $this->quantity->getQuantity();
118
119
        return $quantity ? $this->sum->divide($quantity) : $this->sum;
120
    }
121
122
    /**
123
     * @return int|string
124
     */
125
    public function getId()
126
    {
127
        return $this->id;
128
    }
129
130
    /**
131
     * @return TypeInterface
132
     */
133 1
    public function getType()
134
    {
135 1
        return $this->type;
136
    }
137
138
    /**
139
     * @return DateTimeImmutable
140
     */
141 1
    public function getTime()
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 Money
172
     */
173 1
    public function getSum()
174
    {
175 1
        return $this->sum;
176
    }
177
178
    /**
179
     * @return PlanInterface
180
     */
181 1
    public function getPlan()
182
    {
183 1
        return $this->plan;
184
    }
185
186
    /**
187
     * @return ChargeInterface[]
188
     */
189 1
    public function getCharges()
190
    {
191 1
        return $this->charges;
192
    }
193
194
    public function jsonSerialize()
195
    {
196
        return get_object_vars($this);
197
    }
198
}
199