Passed
Push — main ( 1e6836...3fba09 )
by Iain
05:11
created

Plan::setEntity()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
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\Plan;
16
17
use Parthenon\Billing\Entity\SubscriptionPlan;
18
use Parthenon\Billing\Exception\NoPlanPriceFoundException;
19
use Parthenon\Common\Exception\ParameterNotSetException;
20
21
final class Plan implements PlanInterface
22
{
23
    public const PAY_YEARLY = 'yearly';
24
    public const PAY_MONTHLY = 'monthly';
25
    public const CHECK_FEATURE = 'feature';
26
27
    public function __construct(
28
        private string $name,
29
        private array $limits,
30
        private array $features,
31
        private array $prices,
32
        private bool $isFree,
33
        private bool $isPerSeat,
34
        private int $userCount,
35
        private bool $public = false,
36
        private ?bool $hasTrial = false,
37
        private ?int $trialLengthDays = 0,
38
        private ?SubscriptionPlan $entity = null,
39
    ) {
40
    }
41
42
    public function getName(): string
43
    {
44
        return $this->name;
45
    }
46
47
    public function hasFeature(string $featureName): bool
48
    {
49
        return in_array($featureName, $this->features);
50
    }
51
52
    public function getLimit(LimitableInterface $limitable): int
53
    {
54
        foreach ($this->limits as $name => $limit) {
55
            if ($limitable->getLimitableName() === $name) {
56
                if (!isset($limit['limit'])) {
57
                    throw new ParameterNotSetException('The limit is not set correctly');
58
                }
59
60
                return $limit['limit'];
61
            }
62
        }
63
64
        return -1;
65
    }
66
67
    public function getLimits(): array
68
    {
69
        return $this->limits;
70
    }
71
72
    public function getPriceId(): ?string
73
    {
74
        return $this->priceId;
75
    }
76
77
    public function setPriceId(string $priceId): void
78
    {
79
        $this->priceId = $priceId;
0 ignored issues
show
Bug Best Practice introduced by
The property priceId does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
80
    }
81
82
    /**
83
     * @throws NoPlanPriceFoundException
84
     */
85
    public function getPriceForPaymentSchedule(string $term, string $currency): PlanPrice
86
    {
87
        $currency = strtolower($currency);
88
        if (!isset($this->prices[$term][$currency])) {
89
            throw new NoPlanPriceFoundException(sprintf("No currency '%s' found for '%s' schedule found", $currency, $term));
90
        }
91
92
        return new PlanPrice($term, $this->prices[$term][$currency]['amount'], $currency, $this->prices[$term][$currency]['price_id'] ?? null);
93
    }
94
95
    /**
96
     * @return PlanPrice[]
97
     */
98
    public function getPublicPrices(): array
99
    {
100
        $output = [];
101
        foreach ($this->prices as $term => $currencyData) {
102
            foreach ($currencyData as $currency => $data) {
103
                if (!$data['public']) {
104
                    continue;
105
                }
106
                $output[] = new PlanPrice($term, $data['amount'], $currency, $data['price_id'] ?? null);
107
            }
108
        }
109
110
        return $output;
111
    }
112
113
    public function getFeatures(): array
114
    {
115
        return $this->features;
116
    }
117
118
    public function isFree(): bool
119
    {
120
        return $this->isFree;
121
    }
122
123
    public function isPerSeat(): bool
124
    {
125
        return $this->isPerSeat;
126
    }
127
128
    public function getUserCount(): int
129
    {
130
        return $this->userCount;
131
    }
132
133
    public function setPrices(array $prices): void
134
    {
135
        $this->prices = $prices;
136
    }
137
138
    public function isPublic(): bool
139
    {
140
        return $this->public;
141
    }
142
143
    public function getHasTrial(): ?bool
144
    {
145
        return $this->hasTrial;
146
    }
147
148
    public function setHasTrial(?bool $hasTrial): void
149
    {
150
        $this->hasTrial = $hasTrial;
151
    }
152
153
    public function getTrialLengthDays(): ?int
154
    {
155
        return $this->trialLengthDays;
156
    }
157
158
    public function setTrialLengthDays(?int $trialLengthDays): void
159
    {
160
        $this->trialLengthDays = $trialLengthDays;
161
    }
162
163
    public function getEntity(): ?SubscriptionPlan
164
    {
165
        return $this->entity;
166
    }
167
168
    public function setEntity(?SubscriptionPlan $entity): void
169
    {
170
        $this->entity = $entity;
171
    }
172
173
    public function hasEntity(): bool
174
    {
175
        return isset($this->entity);
176
    }
177
}
178