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