Passed
Push — main ( 3fba09...b0b563 )
by Iain
05:51
created

EmbeddedSubscription::isActive()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 4
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 9
rs 10
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 EmbeddedSubscription
21
{
22
    public const STATUS_UNKNOWN = 'unknown';
23
    public const STATUS_PENDING = 'pending';
24
    public const STATUS_TRIAL = 'trial';
25
    public const STATUS_ACTIVE = 'active';
26
    public const STATUS_CANCELLED = 'cancelled';
27
    public const STATUS_OVERDUE = 'overdue';
28
    public const STATUS_ARRAY = [
29
        self::STATUS_UNKNOWN,
30
        self::STATUS_PENDING,
31
        self::STATUS_TRIAL,
32
        self::STATUS_ACTIVE,
33
        self::STATUS_CANCELLED,
34
        self::STATUS_OVERDUE,
35
    ];
36
37
    public const PAYMENT_SCHEDULE_MONTHLY = 'monthly';
38
    public const PAYMENT_SCHEDULE_YEARLY = 'yearly';
39
    public const PAYMENT_SCHEDULE_LIFETIME = 'lifetime';
40
41
    private ?string $planName = '';
42
43
    private ?string $paymentSchedule;
44
45
    private ?bool $active;
46
47
    private ?string $status = self::STATUS_UNKNOWN;
48
49
    private ?\DateTimeInterface $startedAt;
50
51
    private ?\DateTimeInterface $validUntil;
52
53
    private ?\DateTimeInterface $endedAt;
54
55
    private ?\DateTimeInterface $updatedAt;
56
57
    private ?int $seats;
58
59
    private int $amount;
60
61
    private string $currency;
62
63
    public function getPlanName(): ?string
64
    {
65
        return $this->planName;
66
    }
67
68
    public function setPlanName(string $planName): void
69
    {
70
        $this->planName = $planName;
71
    }
72
73
    public function getPaymentSchedule(): ?string
74
    {
75
        return $this->paymentSchedule;
76
    }
77
78
    public function setPaymentSchedule(string $paymentSchedule): void
79
    {
80
        $this->paymentSchedule = $paymentSchedule;
81
    }
82
83
    public function isActive(): bool
84
    {
85
        if (self::PAYMENT_SCHEDULE_LIFETIME === $this->paymentSchedule) {
86
            return true === $this->active;
87
        }
88
89
        $now = new \DateTime();
90
91
        return true === $this->active && $this->validUntil > $now;
92
    }
93
94
    public function setActive(bool $active): void
95
    {
96
        $this->active = $active;
97
    }
98
99
    public function getStatus(): ?string
100
    {
101
        return $this->status;
102
    }
103
104
    public function setStatus(string $status): void
105
    {
106
        $this->status = $status;
107
    }
108
109
    public function getStartedAt(): ?\DateTimeInterface
110
    {
111
        return $this->startedAt;
112
    }
113
114
    public function setStartedAt(\DateTimeInterface $startedAt): void
115
    {
116
        $this->startedAt = $startedAt;
117
    }
118
119
    public function getValidUntil(): ?\DateTimeInterface
120
    {
121
        return $this->validUntil;
122
    }
123
124
    public function setValidUntil(?\DateTimeInterface $validUntil): void
125
    {
126
        $this->validUntil = $validUntil;
127
    }
128
129
    public function getEndedAt(): ?\DateTimeInterface
130
    {
131
        return $this->endedAt;
132
    }
133
134
    public function setEndedAt(?\DateTimeInterface $endedAt): void
135
    {
136
        $this->endedAt = $endedAt;
137
    }
138
139
    public function getUpdatedAt(): ?\DateTimeInterface
140
    {
141
        return $this->updatedAt;
142
    }
143
144
    public function setUpdatedAt(?\DateTimeInterface $updatedAt): void
145
    {
146
        $this->updatedAt = $updatedAt;
147
    }
148
149
    public function increaseValidUntil()
150
    {
151
        if (self::PAYMENT_SCHEDULE_YEARLY === $this->paymentSchedule) {
152
            $this->validUntil = new \DateTime('+1 Year');
153
        } elseif (self::PAYMENT_SCHEDULE_MONTHLY === $this->paymentSchedule) {
154
            $this->validUntil = new \DateTime('+1 month');
155
        }
156
    }
157
158
    public function getSeats(): ?int
159
    {
160
        return $this->seats;
161
    }
162
163
    public function setSeats(int $seats): void
164
    {
165
        $this->seats = $seats;
166
    }
167
168
    public function getAmount(): int
169
    {
170
        return $this->amount;
171
    }
172
173
    public function setAmount(int $amount): void
174
    {
175
        $this->amount = $amount;
176
    }
177
178
    public function getCurrency(): string
179
    {
180
        return $this->currency;
181
    }
182
183
    public function setCurrency(string $currency): void
184
    {
185
        $this->currency = $currency;
186
    }
187
188
    public function getMoneyAmount(): Money
189
    {
190
        return Money::ofMinor($this->amount, Currency::of($this->currency));
191
    }
192
193
    public function setMoneyAmount(Money $money)
194
    {
195
        $this->amount = $money->getAmount()->getUnscaledValue()->toInt();
196
        $this->currency = $money->getCurrency()->getCurrencyCode();
197
    }
198
}
199