Passed
Branch main (b6a268)
by Iain
04:11
created

Plan::getLimit()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 6
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 13
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * Copyright Humbly Arrogant Ltd 2020-2022.
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.0.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\Subscriptions\Plan;
16
17
use Parthenon\Common\Exception\GeneralException;
18
use Parthenon\Common\Exception\ParameterNotSetException;
19
20
final class Plan
21
{
22
    public const PAY_YEARLY = 'yearly';
23
    public const PAY_MONTHLY = 'monthly';
24
    public const CHECK_FEATURE = 'feature';
25
26
    public function __construct(private string $name, private array $limits, private array $features, private string $yearlyPriceId, private string $monthlyPriceId, private bool $isFree)
27
    {
28
    }
29
30
    public function getName(): string
31
    {
32
        return $this->name;
33
    }
34
35
    public function hasFeature(string $featureName): bool
36
    {
37
        return in_array($featureName, $this->features);
38
    }
39
40
    public function getLimit(LimitableInterface $limitable): int
41
    {
42
        foreach ($this->limits as $name => $limit) {
43
            if ($limitable->getLimitableName() === $name) {
44
                if (!isset($limit['limit'])) {
45
                    throw new ParameterNotSetException('The limit is not set correctly');
46
                }
47
48
                return $limit['limit'];
49
            }
50
        }
51
52
        return -1;
53
    }
54
55
    public function getLimits(): array
56
    {
57
        return $this->limits;
58
    }
59
60
    public function getYearlyPriceId(): string
61
    {
62
        return $this->yearlyPriceId;
63
    }
64
65
    public function setYearlyPriceId(string $yearlyPriceId): void
66
    {
67
        $this->yearlyPriceId = $yearlyPriceId;
68
    }
69
70
    public function getMonthlyPriceId(): string
71
    {
72
        return $this->monthlyPriceId;
73
    }
74
75
    public function setMonthlyPriceId(string $monthlyPriceId): void
76
    {
77
        $this->monthlyPriceId = $monthlyPriceId;
78
    }
79
80
    /**
81
     * @throws \InvalidArgumentException
82
     */
83
    public function getPriceIdForPaymentSchedule(string $term): string
84
    {
85
        switch ($term) {
86
            case static::PAY_YEARLY:
87
                return $this->yearlyPriceId;
88
            case static::PAY_MONTHLY:
89
                return $this->monthlyPriceId;
90
            default:
91
                throw new GeneralException('Only yearly or monthly are currently supported');
92
        }
93
    }
94
95
    public function getFeatures(): array
96
    {
97
        return $this->features;
98
    }
99
100
    public function isFree(): bool
101
    {
102
        return $this->isFree;
103
    }
104
}
105