Passed
Push — main ( 7af6f8...51ae85 )
by Iain
04:22
created

Subscription   A

Complexity

Total Complexity 42

Size/Duplication

Total Lines 256
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 64
dl 0
loc 256
rs 9.0399
c 1
b 0
f 0
wmc 42

42 Methods

Rating   Name   Duplication   Size   Complexity  
A setCurrency() 0 3 1
A getEndedAt() 0 3 1
A setCreatedAt() 0 3 1
A setPrice() 0 3 1
A getUpdatedAt() 0 3 1
A getPaymentSchedule() 0 3 1
A setUpdatedAt() 0 3 1
A getAmount() 0 3 1
A setMainExternalReference() 0 3 1
A getStatus() 0 3 1
A setStatus() 0 3 1
A getPrice() 0 3 1
A getSeats() 0 3 1
A getCreatedAt() 0 3 1
A getSubscriptionPlan() 0 3 1
A setId() 0 3 1
A setPlanName() 0 3 1
A setEndedAt() 0 3 1
A getPlanName() 0 3 1
A getCurrency() 0 3 1
A setSeats() 0 3 1
A setChildExternalReference() 0 3 1
A setAmount() 0 3 1
A setPaymentSchedule() 0 3 1
A getId() 0 3 1
A getMainExternalReference() 0 3 1
A getChildExternalReference() 0 3 1
A setSubscriptionPlan() 0 3 1
A endAtEndOfPeriod() 0 3 1
A getMoneyAmount() 0 3 1
A getValidUntil() 0 3 1
A setPaymentExternalReference() 0 3 1
A setValidUntil() 0 3 1
A getPaymentExternalReference() 0 3 1
A setMainExternalReferenceDetailsUrl() 0 3 1
A setCustomer() 0 3 1
A getMainExternalReferenceDetailsUrl() 0 3 1
A setMoneyAmount() 0 4 1
A isActive() 0 3 1
A setActive() 0 3 1
A getCustomer() 0 3 1
A endNow() 0 4 1

How to fix   Complexity   

Complex Class

Complex classes like Subscription often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Subscription, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * Copyright Iain Cambridge 2020-2023.
7
 *
8
 * Use of this software is governed by the Business Source License included in the LICENSE file and at https://getparthenon.com/docs/next/license.
9
 *
10
 * Change Date: TBD ( 3 years after 2.2.0 release )
11
 *
12
 * On the date above, in accordance with the Business Source License, use of this software will be governed by the open source license specified in the LICENSE file.
13
 */
14
15
namespace Parthenon\Billing\Entity;
16
17
use Brick\Money\Currency;
18
use Brick\Money\Money;
19
20
class Subscription
21
{
22
    private $id;
23
24
    private CustomerInterface $customer;
25
26
    private string $planName;
27
28
    private string $paymentSchedule;
29
30
    private ?int $seats = 1;
31
32
    private bool $active;
33
34
    private string $status;
35
36
    private ?int $amount = null;
37
38
    private ?string $currency = null;
39
40
    private string $mainExternalReference;
41
42
    private ?string $mainExternalReferenceDetailsUrl = null;
43
44
    private string $childExternalReference;
45
46
    private string $paymentExternalReference;
47
48
    private ?SubscriptionPlan $subscriptionPlan = null;
49
50
    private ?Price $price = null;
51
52
    private \DateTimeInterface $createdAt;
53
54
    private ?\DateTimeInterface $validUntil = null;
55
56
    private \DateTimeInterface $updatedAt;
57
58
    private ?\DateTimeInterface $endedAt = null;
59
60
    /**
61
     * @return mixed
62
     */
63
    public function getId()
64
    {
65
        return $this->id;
66
    }
67
68
    /**
69
     * @param mixed $id
70
     */
71
    public function setId($id): void
72
    {
73
        $this->id = $id;
74
    }
75
76
    public function getPlanName(): string
77
    {
78
        return $this->planName;
79
    }
80
81
    public function setPlanName(string $planName): void
82
    {
83
        $this->planName = $planName;
84
    }
85
86
    public function getPaymentSchedule(): string
87
    {
88
        return $this->paymentSchedule;
89
    }
90
91
    public function setPaymentSchedule(string $paymentSchedule): void
92
    {
93
        $this->paymentSchedule = $paymentSchedule;
94
    }
95
96
    public function getSeats(): ?int
97
    {
98
        return $this->seats;
99
    }
100
101
    public function setSeats(?int $seats): void
102
    {
103
        $this->seats = $seats;
104
    }
105
106
    public function getStatus(): string
107
    {
108
        return $this->status;
109
    }
110
111
    public function setStatus(string $status): void
112
    {
113
        $this->status = $status;
114
    }
115
116
    public function getSubscriptionPlan(): ?SubscriptionPlan
117
    {
118
        return $this->subscriptionPlan;
119
    }
120
121
    public function setSubscriptionPlan(?SubscriptionPlan $subscriptionPlan): void
122
    {
123
        $this->subscriptionPlan = $subscriptionPlan;
124
    }
125
126
    public function getMainExternalReference(): string
127
    {
128
        return $this->mainExternalReference;
129
    }
130
131
    public function setMainExternalReference(string $mainExternalReference): void
132
    {
133
        $this->mainExternalReference = $mainExternalReference;
134
    }
135
136
    public function getChildExternalReference(): string
137
    {
138
        return $this->childExternalReference;
139
    }
140
141
    public function setChildExternalReference(string $childExternalReference): void
142
    {
143
        $this->childExternalReference = $childExternalReference;
144
    }
145
146
    public function getAmount(): ?int
147
    {
148
        return $this->amount;
149
    }
150
151
    public function setAmount(?int $amount): void
152
    {
153
        $this->amount = $amount;
154
    }
155
156
    public function getCurrency(): ?string
157
    {
158
        return $this->currency;
159
    }
160
161
    public function setCurrency(?string $currency): void
162
    {
163
        $this->currency = $currency;
164
    }
165
166
    public function getPrice(): ?Price
167
    {
168
        return $this->price;
169
    }
170
171
    public function setPrice(?Price $price): void
172
    {
173
        $this->price = $price;
174
    }
175
176
    public function getCreatedAt(): \DateTimeInterface
177
    {
178
        return $this->createdAt;
179
    }
180
181
    public function setCreatedAt(\DateTimeInterface $createdAt): void
182
    {
183
        $this->createdAt = $createdAt;
184
    }
185
186
    public function getUpdatedAt(): \DateTimeInterface
187
    {
188
        return $this->updatedAt;
189
    }
190
191
    public function setUpdatedAt(\DateTimeInterface $updatedAt): void
192
    {
193
        $this->updatedAt = $updatedAt;
194
    }
195
196
    public function getEndedAt(): ?\DateTimeInterface
197
    {
198
        return $this->endedAt;
199
    }
200
201
    public function setEndedAt(?\DateTimeInterface $endedAt): void
202
    {
203
        $this->endedAt = $endedAt;
204
    }
205
206
    public function endAtEndOfPeriod(): void
207
    {
208
        $this->endedAt = clone $this->validUntil;
209
    }
210
211
    public function endNow(): void
212
    {
213
        $this->endedAt = new \DateTime('now');
214
        $this->validUntil = new \DateTime('now');
215
    }
216
217
    public function isActive(): bool
218
    {
219
        return $this->active;
220
    }
221
222
    public function setActive(bool $active): void
223
    {
224
        $this->active = $active;
225
    }
226
227
    public function getValidUntil(): \DateTimeInterface
228
    {
229
        return $this->validUntil;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->validUntil could return the type null which is incompatible with the type-hinted return DateTimeInterface. Consider adding an additional type-check to rule them out.
Loading history...
230
    }
231
232
    public function setValidUntil(\DateTimeInterface $validUntil): void
233
    {
234
        $this->validUntil = $validUntil;
235
    }
236
237
    public function getMoneyAmount(): Money
238
    {
239
        return Money::ofMinor($this->amount, Currency::of($this->currency));
240
    }
241
242
    public function setMoneyAmount(Money $money)
243
    {
244
        $this->amount = $money->getAmount()->getUnscaledValue()->toInt();
245
        $this->currency = $money->getCurrency()->getCurrencyCode();
246
    }
247
248
    public function getCustomer(): CustomerInterface
249
    {
250
        return $this->customer;
251
    }
252
253
    public function setCustomer(CustomerInterface $customer): void
254
    {
255
        $this->customer = $customer;
256
    }
257
258
    public function getMainExternalReferenceDetailsUrl(): ?string
259
    {
260
        return $this->mainExternalReferenceDetailsUrl;
261
    }
262
263
    public function setMainExternalReferenceDetailsUrl(?string $mainExternalReferenceDetailsUrl): void
264
    {
265
        $this->mainExternalReferenceDetailsUrl = $mainExternalReferenceDetailsUrl;
266
    }
267
268
    public function getPaymentExternalReference(): string
269
    {
270
        return $this->paymentExternalReference;
271
    }
272
273
    public function setPaymentExternalReference(?string $paymentExternalReference): void
274
    {
275
        $this->paymentExternalReference = $paymentExternalReference;
276
    }
277
}
278