Passed
Push — main ( b21dd6...3eee1d )
by Iain
07:22 queued 02:58
created

Subscription   C

Complexity

Total Complexity 53

Size/Duplication

Total Lines 305
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 82
c 2
b 0
f 0
dl 0
loc 305
rs 6.96
wmc 53

48 Methods

Rating   Name   Duplication   Size   Complexity  
A setTrialLengthDays() 0 3 1
A setStartOfCurrentPeriod() 0 3 1
A getTrialLengthDays() 0 3 1
A getStartOfCurrentPeriod() 0 3 1
A isHasTrial() 0 3 1
A setHasTrial() 0 3 1
A getPaymentSchedule() 0 3 1
A getStatus() 0 3 1
A getSeats() 0 3 1
A setId() 0 3 1
A setPlanName() 0 3 1
A getPlanName() 0 3 1
A setSeats() 0 3 1
A getId() 0 3 1
A setPaymentSchedule() 0 3 1
A getMoneyAmount() 0 3 1
A getValidUntil() 0 3 1
A setCurrency() 0 3 1
A getEndedAt() 0 3 1
A setPaymentDetails() 0 3 1
A setCreatedAt() 0 3 1
A setPrice() 0 3 1
A getUpdatedAt() 0 3 1
A setValidUntil() 0 3 1
A setUpdatedAt() 0 3 1
A getAmount() 0 3 1
A setMainExternalReference() 0 3 1
A setStatus() 0 12 5
A getPrice() 0 3 1
A setMainExternalReferenceDetailsUrl() 0 3 1
A getCreatedAt() 0 3 1
A setCustomer() 0 3 1
A getMainExternalReferenceDetailsUrl() 0 3 1
A setMoneyAmount() 0 8 2
A getSubscriptionPlan() 0 3 1
A setEndedAt() 0 3 1
A getCurrency() 0 3 1
A getPaymentDetails() 0 3 1
A isActive() 0 3 1
A setActive() 0 3 1
A setChildExternalReference() 0 3 1
A endAtEndOfPeriod() 0 3 1
A setAmount() 0 3 1
A getCustomer() 0 3 1
A getMainExternalReference() 0 3 1
A setSubscriptionPlan() 0 3 1
A getChildExternalReference() 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
use Parthenon\Billing\Enum\SubscriptionStatus;
20
21
class Subscription
22
{
23
    private $id;
24
25
    private CustomerInterface $customer;
26
27
    private string $planName;
28
29
    private string $paymentSchedule;
30
31
    private ?int $seats = 1;
32
33
    private bool $active;
34
35
    private SubscriptionStatus $status;
36
37
    private ?int $amount = null;
38
39
    private ?string $currency = null;
40
41
    private string $mainExternalReference;
42
43
    private ?string $mainExternalReferenceDetailsUrl = null;
44
45
    private string $childExternalReference;
46
47
    private ?PaymentDetails $paymentDetails = null;
48
49
    private ?SubscriptionPlan $subscriptionPlan = null;
50
51
    private ?Price $price = null;
52
53
    private \DateTimeInterface $createdAt;
54
55
    private ?\DateTimeInterface $startOfCurrentPeriod = null;
56
57
    private ?\DateTimeInterface $validUntil = null;
58
59
    private \DateTimeInterface $updatedAt;
60
61
    private ?\DateTimeInterface $endedAt = null;
62
63
    private bool $hasTrial = false;
64
65
    private ?int $trialLengthDays = 0;
66
67
    /**
68
     * @return mixed
69
     */
70
    public function getId()
71
    {
72
        return $this->id;
73
    }
74
75
    /**
76
     * @param mixed $id
77
     */
78
    public function setId($id): void
79
    {
80
        $this->id = $id;
81
    }
82
83
    public function getPlanName(): string
84
    {
85
        return $this->planName;
86
    }
87
88
    public function setPlanName(string $planName): void
89
    {
90
        $this->planName = $planName;
91
    }
92
93
    public function getPaymentSchedule(): string
94
    {
95
        return $this->paymentSchedule;
96
    }
97
98
    public function setPaymentSchedule(string $paymentSchedule): void
99
    {
100
        $this->paymentSchedule = $paymentSchedule;
101
    }
102
103
    public function getSeats(): ?int
104
    {
105
        return $this->seats;
106
    }
107
108
    public function setSeats(?int $seats): void
109
    {
110
        $this->seats = $seats;
111
    }
112
113
    public function getStatus(): SubscriptionStatus
114
    {
115
        return $this->status;
116
    }
117
118
    public function setStatus(SubscriptionStatus $status): void
119
    {
120
        if (SubscriptionStatus::CANCELLED === $status ||
121
            SubscriptionStatus::PAUSED === $status ||
122
            SubscriptionStatus::BLOCKED === $status ||
123
            SubscriptionStatus::OVERDUE_PAYMENT_DISABLED === $status) {
124
            $this->active = false;
125
        } else {
126
            $this->active = true;
127
        }
128
129
        $this->status = $status;
130
    }
131
132
    public function getSubscriptionPlan(): ?SubscriptionPlan
133
    {
134
        return $this->subscriptionPlan;
135
    }
136
137
    public function setSubscriptionPlan(?SubscriptionPlan $subscriptionPlan): void
138
    {
139
        $this->subscriptionPlan = $subscriptionPlan;
140
    }
141
142
    public function getMainExternalReference(): string
143
    {
144
        return $this->mainExternalReference;
145
    }
146
147
    public function setMainExternalReference(string $mainExternalReference): void
148
    {
149
        $this->mainExternalReference = $mainExternalReference;
150
    }
151
152
    public function getChildExternalReference(): string
153
    {
154
        return $this->childExternalReference;
155
    }
156
157
    public function setChildExternalReference(string $childExternalReference): void
158
    {
159
        $this->childExternalReference = $childExternalReference;
160
    }
161
162
    public function getAmount(): ?int
163
    {
164
        return $this->amount;
165
    }
166
167
    public function setAmount(?int $amount): void
168
    {
169
        $this->amount = $amount;
170
    }
171
172
    public function getCurrency(): ?string
173
    {
174
        return $this->currency;
175
    }
176
177
    public function setCurrency(?string $currency): void
178
    {
179
        $this->currency = $currency;
180
    }
181
182
    public function getPrice(): ?Price
183
    {
184
        return $this->price;
185
    }
186
187
    public function setPrice(?Price $price): void
188
    {
189
        $this->price = $price;
190
    }
191
192
    public function getCreatedAt(): \DateTimeInterface
193
    {
194
        return $this->createdAt;
195
    }
196
197
    public function setCreatedAt(\DateTimeInterface $createdAt): void
198
    {
199
        $this->createdAt = $createdAt;
200
    }
201
202
    public function getUpdatedAt(): \DateTimeInterface
203
    {
204
        return $this->updatedAt;
205
    }
206
207
    public function setUpdatedAt(\DateTimeInterface $updatedAt): void
208
    {
209
        $this->updatedAt = $updatedAt;
210
    }
211
212
    public function getEndedAt(): ?\DateTimeInterface
213
    {
214
        return $this->endedAt;
215
    }
216
217
    public function setEndedAt(?\DateTimeInterface $endedAt): void
218
    {
219
        $this->endedAt = $endedAt;
220
    }
221
222
    public function endAtEndOfPeriod(): void
223
    {
224
        $this->endedAt = clone $this->validUntil;
225
    }
226
227
    public function endNow(): void
228
    {
229
        $this->endedAt = new \DateTime('now');
230
        $this->validUntil = new \DateTime('now');
231
    }
232
233
    public function isActive(): bool
234
    {
235
        return $this->active;
236
    }
237
238
    public function setActive(bool $active): void
239
    {
240
        $this->active = $active;
241
    }
242
243
    public function getValidUntil(): \DateTimeInterface
244
    {
245
        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...
246
    }
247
248
    public function setValidUntil(\DateTimeInterface $validUntil): void
249
    {
250
        $this->validUntil = $validUntil;
251
    }
252
253
    public function getMoneyAmount(): Money
254
    {
255
        return Money::ofMinor($this->amount, Currency::of($this->currency));
256
    }
257
258
    public function setMoneyAmount(?Money $money)
259
    {
260
        if (!$money) {
261
            return;
262
        }
263
264
        $this->amount = $money->getMinorAmount()->toInt();
265
        $this->currency = $money->getCurrency()->getCurrencyCode();
266
    }
267
268
    public function getCustomer(): CustomerInterface
269
    {
270
        return $this->customer;
271
    }
272
273
    public function setCustomer(CustomerInterface $customer): void
274
    {
275
        $this->customer = $customer;
276
    }
277
278
    public function getMainExternalReferenceDetailsUrl(): ?string
279
    {
280
        return $this->mainExternalReferenceDetailsUrl;
281
    }
282
283
    public function setMainExternalReferenceDetailsUrl(?string $mainExternalReferenceDetailsUrl): void
284
    {
285
        $this->mainExternalReferenceDetailsUrl = $mainExternalReferenceDetailsUrl;
286
    }
287
288
    public function getStartOfCurrentPeriod(): ?\DateTimeInterface
289
    {
290
        return $this->startOfCurrentPeriod;
291
    }
292
293
    public function setStartOfCurrentPeriod(?\DateTimeInterface $startOfCurrentPeriod): void
294
    {
295
        $this->startOfCurrentPeriod = $startOfCurrentPeriod;
296
    }
297
298
    public function isHasTrial(): bool
299
    {
300
        return $this->hasTrial;
301
    }
302
303
    public function setHasTrial(bool $hasTrial): void
304
    {
305
        $this->hasTrial = $hasTrial;
306
    }
307
308
    public function getTrialLengthDays(): ?int
309
    {
310
        return $this->trialLengthDays;
311
    }
312
313
    public function setTrialLengthDays(?int $trialLengthDays): void
314
    {
315
        $this->trialLengthDays = $trialLengthDays;
316
    }
317
318
    public function getPaymentDetails(): ?PaymentDetails
319
    {
320
        return $this->paymentDetails;
321
    }
322
323
    public function setPaymentDetails(?PaymentDetails $paymentDetails): void
324
    {
325
        $this->paymentDetails = $paymentDetails;
326
    }
327
}
328