PlanController::listAction()   A
last analyzed

Complexity

Conditions 5
Paths 7

Size

Total Lines 45
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 28
c 1
b 0
f 0
nc 7
nop 4
dl 0
loc 45
rs 9.1608
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\Controller;
23
24
use Parthenon\Billing\CustomerProviderInterface;
25
use Parthenon\Billing\Exception\NoCustomerException;
26
use Parthenon\Billing\Plan\Plan;
27
use Parthenon\Billing\Plan\PlanManagerInterface;
28
use Parthenon\Billing\Subscription\SubscriptionProviderInterface;
29
use Parthenon\Common\LoggerAwareTrait;
30
use Psr\Log\LoggerInterface;
31
use Symfony\Component\HttpFoundation\JsonResponse;
32
use Symfony\Component\Routing\Attribute\Route;
33
34
class PlanController
35
{
36
    use LoggerAwareTrait;
37
38
    #[Route('/billing/plans', name: 'parthenon_billing_plan_list_h')]
39
    public function listAction(
40
        PlanManagerInterface $planManager,
41
        CustomerProviderInterface $customerProvider,
42
        SubscriptionProviderInterface $subscriptionProvider,
43
        LoggerInterface $logger,
0 ignored issues
show
Unused Code introduced by
The parameter $logger is not used and could be removed. ( Ignorable by Annotation )

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

43
        /** @scrutinizer ignore-unused */ LoggerInterface $logger,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
44
    ) {
45
        $this->getLogger()->info('Getting plans info');
46
        $plans = $planManager->getPlans();
47
48
        $output = [];
49
        $currentPlanOutput = [];
50
        try {
51
            $currentCustomer = $customerProvider->getCurrentCustomer();
52
        } catch (NoCustomerException $exception) {
53
            $this->getLogger()->error('No customer found');
54
55
            return new JsonResponse([], JsonResponse::HTTP_INTERNAL_SERVER_ERROR);
56
        }
57
58
        $subscriptions = $subscriptionProvider->getSubscriptionsForCustomer($currentCustomer);
59
        foreach ($subscriptions as $subscription) {
60
            $currentPlanOutput[] = [
61
                'name' => $subscription->getPlanName(),
62
                'schedule' => $subscription->getPaymentSchedule(),
63
                'id' => (string) $subscription->getId(),
64
                'currency' => $subscription->getCurrency(),
65
            ];
66
        }
67
68
        foreach ($plans as $plan) {
69
            if (!$plan->isPublic()) {
70
                continue;
71
            }
72
            $output[$plan->getName()] = [
73
                'name' => $plan->getName(),
74
                'limits' => $plan->getLimits(),
75
                'features' => $plan->getFeatures(),
76
                'prices' => $this->generateSchedules($plan),
77
            ];
78
        }
79
80
        return new JsonResponse([
81
            'plans' => $output,
82
            'current_plans' => $currentPlanOutput,
83
        ]);
84
    }
85
86
    private function generateSchedules(Plan $plan): array
87
    {
88
        $output = [];
89
90
        foreach ($plan->getPublicPrices() as $data) {
91
            $output[$data->getSchedule()][strtoupper($data->getCurrency())] = [
92
                'schedule' => $data->getSchedule(),
93
                'amount' => $data->getAsMoney()->getAmount(),
94
                'currency' => strtoupper($data->getCurrency()),
95
            ];
96
        }
97
98
        return $output;
99
    }
100
}
101