Passed
Push — main ( 241451...172c2c )
by Iain
05:00
created

BillaBearPlanManager::convertLimits()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 14
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * Copyright (C) 2020-2024 Iain Cambridge
7
 *
8
 *     This program is free software: you can redistribute it and/or modify
9
 *     it under the terms of the GNU General Public License as published by
10
 *     the Free Software Foundation, either version 3 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 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\BillaBear\Plan;
23
24
use BillaBear\Model\Feature;
0 ignored issues
show
Bug introduced by
The type BillaBear\Model\Feature was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
use BillaBear\Model\Limit;
0 ignored issues
show
Bug introduced by
The type BillaBear\Model\Limit was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
use BillaBear\Model\Price;
0 ignored issues
show
Bug introduced by
The type BillaBear\Model\Price was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
use BillaBear\Model\SubscriptionPlan;
0 ignored issues
show
Bug introduced by
The type BillaBear\Model\SubscriptionPlan was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
28
use Doctrine\Common\Collections\Collection;
29
use Parthenon\Billing\BillaBear\SdkFactory;
30
use Parthenon\Billing\Exception\NoPlanFoundException;
31
use Parthenon\Billing\Plan\LimitedUserInterface;
32
use Parthenon\Billing\Plan\Plan;
33
use Parthenon\Billing\Plan\PlanManagerInterface;
34
35
final class BillaBearPlanManager implements PlanManagerInterface
36
{
37
    private ?array $plans = null;
38
39
    public function __construct(
40
        private SdkFactory $sdkFactory,
41
    ) {
42
    }
43
44
    public function getPlans(): array
45
    {
46
        if (!isset($this->plans)) {
47
            $data = $this->sdkFactory->createSubscriptionsApi()->listSubscriptionPlans(limit: 100);
48
49
            $output = [];
50
            foreach ($data->getData() as $planEntity) {
51
                $output[] = $this->convertPlan($planEntity);
52
            }
53
            $this->plans = $output;
54
        }
55
56
        return $output;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $output does not seem to be defined for all execution paths leading up to this point.
Loading history...
57
    }
58
59
    public function getPlanForUser(LimitedUserInterface $limitedUser): Plan
60
    {
61
        return $this->getPlanByName($limitedUser->getPlanName());
0 ignored issues
show
Bug introduced by
It seems like $limitedUser->getPlanName() can also be of type null; however, parameter $planName of Parthenon\Billing\BillaB...anager::getPlanByName() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

61
        return $this->getPlanByName(/** @scrutinizer ignore-type */ $limitedUser->getPlanName());
Loading history...
62
    }
63
64
    public function getPlanByName(string $planName): Plan
65
    {
66
        $plans = $this->getPlans();
67
68
        foreach ($plans as $plan) {
69
            if ($plan->getName() === $planName) {
70
                return $plan;
71
            }
72
        }
73
        throw new NoPlanFoundException();
74
    }
75
76
    protected function convertPlan(SubscriptionPlan $subscriptionPlan): Plan
77
    {
78
        return new Plan(
79
            $subscriptionPlan->getName(),
80
            $this->convertLimits($subscriptionPlan->getLimits()),
81
            $this->convertFeatures($subscriptionPlan->getFeatures()),
82
            $this->convertPrices($subscriptionPlan->getPrices()),
83
            $subscriptionPlan->getFree(),
84
            $subscriptionPlan->getPerSeat(),
85
            $subscriptionPlan->getUserCount(),
86
            $subscriptionPlan->getPublic(),
87
            $subscriptionPlan->getHasTrial(),
88
            $subscriptionPlan->getTrialLengthDays(),
89
            $subscriptionPlan->getId()
90
        );
91
    }
92
93
    /**
94
     * @param Limit[] $limits
95
     */
96
    protected function convertLimits(array $limits): array
97
    {
98
        $output = [];
99
100
        foreach ($limits as $limit) {
101
            $output[$limit->getFeature()->getCode()] = [
102
                'name' => $limit->getFeature()->getName(),
103
                'code' => $limit->getFeature()->getCode(),
104
                'limit' => $limit->getLimit(),
105
                'description' => $limit->getFeature()->getDescription(),
106
            ];
107
        }
108
109
        return $output;
110
    }
111
112
    /**
113
     * @param Feature[]|Collection $features
114
     */
115
    protected function convertFeatures(array $features): array
116
    {
117
        $output = [];
118
119
        foreach ($features as $feature) {
120
            $output[$feature->getCode()] = [
121
                'code' => $feature->getCode(),
122
                'name' => $feature->getName(),
123
                'description' => $feature->getDescription(),
124
            ];
125
        }
126
127
        return $output;
128
    }
129
130
    /**
131
     * @param Price[]|Collection $prices
132
     */
133
    protected function convertPrices(array $prices): array
134
    {
135
        $output = [];
136
137
        foreach ($prices as $price) {
138
            $schedule = $price->getSchedule();
139
140
            if (!isset($output[$schedule])) {
141
                $output[$schedule] = [];
142
            }
143
144
            $output[$schedule][$price->getCurrency()] = [
145
                'amount' => (string) $price->getAmount(),
146
                'currency' => $price->getCurrency(),
147
                'price_id' => $price->getExternalReference(),
148
                'public' => $price->getPublic(),
149
                'entity_id' => $price->getId(),
150
            ];
151
        }
152
153
        return $output;
154
    }
155
}
156