SubscriptionPlan::getPriceForCurrencyAndSchedule()   A
last analyzed

Complexity

Conditions 4
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 4
c 1
b 0
f 0
nc 3
nop 2
dl 0
loc 8
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * Copyright (C) 2020-2025 Iain Cambridge
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU LESSER GENERAL PUBLIC LICENSE as published by
10
 * the Free Software Foundation, either version 2.1 of the License, or
11
 * (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Lesser General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU General Public License
19
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20
 */
21
22
namespace Parthenon\Billing\Entity;
23
24
use Doctrine\Common\Collections\ArrayCollection;
25
use Doctrine\Common\Collections\Collection;
26
use Parthenon\Athena\Entity\CrudEntityInterface;
27
28
class SubscriptionPlan implements CrudEntityInterface, SubscriptionPlanInterface
29
{
30
    private $id;
31
32
    private bool $public = false;
33
34
    private string $name;
35
36
    private ?string $codeName = null;
37
38
    private ?string $externalReference = null;
39
40
    private ?string $paymentProviderDetailsLink = null;
41
42
    private array|Collection $limits;
43
44
    private bool $perSeat;
45
46
    private bool $free;
47
48
    private int $userCount;
49
50
    private ?bool $hasTrial = false;
51
52
    private ?int $trialLengthDays = 0;
53
54
    private array|Collection $features;
55
56
    private array|Collection $prices;
57
58
    private ProductInterface $product;
59
60
    public function __construct()
61
    {
62
        $this->limits = new ArrayCollection();
63
        $this->features = new ArrayCollection();
64
        $this->prices = new ArrayCollection();
65
    }
66
67
    public function getId()
68
    {
69
        return $this->id;
70
    }
71
72
    public function setId($id): void
73
    {
74
        $this->id = $id;
75
    }
76
77
    public function isPublic(): bool
78
    {
79
        return $this->public;
80
    }
81
82
    public function setPublic(bool $public): void
83
    {
84
        $this->public = $public;
85
    }
86
87
    public function getName(): string
88
    {
89
        return $this->name;
90
    }
91
92
    public function setName(string $name): void
93
    {
94
        $this->name = $name;
95
    }
96
97
    public function getCodeName(): ?string
98
    {
99
        return $this->codeName;
100
    }
101
102
    public function setCodeName(?string $codeName): void
103
    {
104
        $this->codeName = $codeName;
105
    }
106
107
    public function getExternalReference(): ?string
108
    {
109
        return $this->externalReference;
110
    }
111
112
    public function setExternalReference(string $externalReference): void
113
    {
114
        $this->externalReference = $externalReference;
115
    }
116
117
    public function hasExternalReference(): bool
118
    {
119
        return isset($this->externalReference);
120
    }
121
122
    public function getPaymentProviderDetailsLink(): ?string
123
    {
124
        return $this->paymentProviderDetailsLink;
125
    }
126
127
    public function setPaymentProviderDetailsLink(?string $paymentProviderDetailsLink): void
128
    {
129
        $this->paymentProviderDetailsLink = $paymentProviderDetailsLink;
130
    }
131
132
    public function getLimits(): Collection|array
133
    {
134
        return $this->limits;
135
    }
136
137
    /**
138
     * @param SubscriptionPlanLimit[]|Collection $limits
139
     */
140
    public function setLimits(Collection|array $limits): void
141
    {
142
        $this->limits = $limits;
143
    }
144
145
    public function addLimit(SubscriptionPlanLimit $limit): void
146
    {
147
        if (!$this->limits->contains($limit)) {
148
            $limit->setSubscriptionPlan($this);
149
            $this->limits->add($limit);
150
        }
151
    }
152
153
    public function removeLimit(SubscriptionPlanLimit $limit): void
154
    {
155
        $this->limits->removeElement($limit);
156
    }
157
158
    public function addFeature(SubscriptionFeature $subscriptionFeature): void
159
    {
160
        if (!$this->features->contains($subscriptionFeature)) {
161
            $this->features->add($subscriptionFeature);
162
        }
163
    }
164
165
    public function removeFeature(SubscriptionFeature $subscriptionFeature): void
166
    {
167
        $this->features->removeElement($subscriptionFeature);
168
    }
169
170
    public function addPrice(Price $price): void
171
    {
172
        if (!$this->prices->contains($price)) {
173
            $this->prices->add($price);
174
        }
175
    }
176
177
    public function getPriceForCurrencyAndSchedule(string $currency, string $schedule): Price
178
    {
179
        foreach ($this->getPrices() as $price) {
180
            if (strtolower($price->getCurrency()) === strtolower($currency) && strtolower($price->getSchedule()) === strtolower($schedule)) {
181
                return $price;
182
            }
183
        }
184
        throw new \Exception("Can't found price");
185
    }
186
187
    public function removePrice(Price $price): void
188
    {
189
        $this->prices->removeElement($price);
190
    }
191
192
    public function getPrices(): Collection|array
193
    {
194
        return $this->prices;
195
    }
196
197
    public function setPrices(Collection|array $prices): void
198
    {
199
        $this->prices = $prices;
200
    }
201
202
    public function getDisplayName(): string
203
    {
204
        return $this->name;
205
    }
206
207
    public function isPerSeat(): bool
208
    {
209
        return $this->perSeat;
210
    }
211
212
    public function setPerSeat(bool $perSeat): void
213
    {
214
        $this->perSeat = $perSeat;
215
    }
216
217
    public function isFree(): bool
218
    {
219
        return $this->free;
220
    }
221
222
    public function setFree(bool $free): void
223
    {
224
        $this->free = $free;
225
    }
226
227
    public function getUserCount(): int
228
    {
229
        return $this->userCount;
230
    }
231
232
    public function setUserCount(int $userCount): void
233
    {
234
        $this->userCount = $userCount;
235
    }
236
237
    public function getFeatures(): Collection|array
238
    {
239
        return $this->features;
240
    }
241
242
    public function setFeatures(Collection|array $features): void
243
    {
244
        $this->features = $features;
245
    }
246
247
    public function getProduct(): ProductInterface
248
    {
249
        return $this->product;
250
    }
251
252
    public function setProduct(ProductInterface $product): void
253
    {
254
        $this->product = $product;
255
    }
256
257
    public function getHasTrial(): bool
258
    {
259
        return true === $this->hasTrial;
260
    }
261
262
    public function setHasTrial(?bool $hasTrial): void
263
    {
264
        $this->hasTrial = $hasTrial;
265
    }
266
267
    public function getTrialLengthDays(): int
268
    {
269
        return (int) $this->trialLengthDays;
270
    }
271
272
    public function setTrialLengthDays(?int $trialLengthDays): void
273
    {
274
        $this->trialLengthDays = $trialLengthDays;
275
    }
276
}
277