AthenaController::cancelAndRefundSubscription()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 24
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 10
c 0
b 0
f 0
nc 2
nop 6
dl 0
loc 24
rs 9.9332
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\Entity\Subscription;
25
use Parthenon\Billing\Refund\RefundManagerInterface;
26
use Parthenon\Billing\Repository\SubscriptionRepositoryInterface;
27
use Parthenon\Billing\Subscription\SubscriptionManagerInterface;
28
use Parthenon\Common\Exception\NoEntityFoundException;
29
use Symfony\Bundle\SecurityBundle\Security;
30
use Symfony\Component\HttpFoundation\RedirectResponse;
31
use Symfony\Component\HttpFoundation\Request;
32
use Symfony\Component\HttpFoundation\Response;
33
use Symfony\Component\Routing\Annotation\Route;
34
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
35
36
class AthenaController
37
{
38
    #[Route('/athena/billing/subscription/{id}/cancel', name: 'parthenon_billing_athena_subscription_cancel', methods: ['GET'])]
39
    public function cancelSubscription(
40
        Request $request,
41
        SubscriptionRepositoryInterface $subscriptionRepository,
42
        SubscriptionManagerInterface $subscriptionManager,
43
        UrlGeneratorInterface $urlGenerator,
44
    ): Response {
45
        try {
46
            /** @var Subscription $subscription */
47
            $subscription = $subscriptionRepository->findById($request->get('id'));
48
        } catch (NoEntityFoundException $e) {
49
            return new RedirectResponse($urlGenerator->generate('parthenon_athena_crud_subscriptions_list'));
50
        }
51
        $subscriptionManager->cancelSubscriptionAtEndOfCurrentPeriod($subscription);
52
        $subscriptionRepository->save($subscription);
53
        $request->getSession()->getFlashBag()->add('success', 'Cancelled');
54
55
        return new RedirectResponse($urlGenerator->generate('parthenon_athena_crud_subscriptions_read', ['id' => $request->get('id')]));
56
    }
57
58
    #[Route('/athena/billing/subscription/{id}/cancel-refund', name: 'parthenon_billing_athena_subscription_cancel_refund', methods: ['GET'])]
59
    public function cancelAndRefundSubscription(
60
        Request $request,
61
        SubscriptionRepositoryInterface $subscriptionRepository,
62
        SubscriptionManagerInterface $subscriptionManager,
63
        RefundManagerInterface $refundManager,
64
        UrlGeneratorInterface $urlGenerator,
65
        Security $security,
66
    ): Response {
67
        try {
68
            /** @var Subscription $subscription */
69
            $subscription = $subscriptionRepository->findById($request->get('id'));
70
        } catch (NoEntityFoundException $e) {
71
            return new RedirectResponse($urlGenerator->generate('parthenon_athena_crud_subscriptions_list'));
72
        }
73
74
        $subscriptionManager->cancelSubscriptionInstantly($subscription);
75
        $subscriptionRepository->save($subscription);
76
77
        $refundManager->issueFullRefundForSubscription($subscription, $security->getUser());
0 ignored issues
show
Bug introduced by
$security->getUser() of type Symfony\Component\Securi...User\UserInterface|null is incompatible with the type Parthenon\Billing\Entity\BillingAdminInterface expected by parameter $billingAdmin of Parthenon\Billing\Refund...RefundForSubscription(). ( Ignorable by Annotation )

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

77
        $refundManager->issueFullRefundForSubscription($subscription, /** @scrutinizer ignore-type */ $security->getUser());
Loading history...
78
79
        $request->getSession()->getFlashBag()->add('success', 'Cancelled and refunded');
80
81
        return new RedirectResponse($urlGenerator->generate('parthenon_athena_crud_subscriptions_read', ['id' => $request->get('id')]));
82
    }
83
}
84