Passed
Push — main ( 350a1e...a10163 )
by Iain
05:08
created

AthenaController::cancelAndRefundSubscription()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 9
c 0
b 0
f 0
nc 2
nop 5
dl 0
loc 19
rs 9.9666
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\Controller;
16
17
use Parthenon\Billing\Entity\Subscription;
18
use Parthenon\Billing\Repository\SubscriptionRepositoryInterface;
19
use Parthenon\Billing\Subscription\SubscriptionManagerInterface;
20
use Parthenon\Common\Exception\NoEntityFoundException;
21
use Symfony\Bundle\SecurityBundle\Security;
22
use Symfony\Component\HttpFoundation\RedirectResponse;
23
use Symfony\Component\HttpFoundation\Request;
24
use Symfony\Component\HttpFoundation\Response;
25
use Symfony\Component\Routing\Annotation\Route;
26
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
27
28
class AthenaController
29
{
30
    #[Route('/athena/billing/subscription/{id}/cancel', name: 'parthenon_billing_athena_subscription_cancel', methods: ['GET'])]
31
    public function cancelSubscription(
32
        Request $request,
33
        SubscriptionRepositoryInterface $subscriptionRepository,
34
        SubscriptionManagerInterface $subscriptionManager,
35
        UrlGeneratorInterface $urlGenerator
36
    ): Response {
37
        try {
38
            /** @var Subscription $subscription */
39
            $subscription = $subscriptionRepository->findById($request->get('id'));
40
        } catch (NoEntityFoundException $e) {
41
            return new RedirectResponse($urlGenerator->generate('parthenon_athena_crud_subscriptions_list'));
42
        }
43
        $subscriptionManager->cancelSubscriptionAtEndOfCurrentPeriod($subscription);
44
        $subscriptionRepository->save($subscription);
45
        $request->getSession()->getFlashBag()->add('success', 'Cancelled');
46
47
        return new RedirectResponse($urlGenerator->generate('parthenon_athena_crud_subscriptions_read', ['id' => $request->get('id')]));
48
    }
49
50
    #[Route('/athena/billing/subscription/{id}/cancel-refund', name: 'parthenon_billing_athena_subscription_cancel_refund', methods: ['GET'])]
51
    public function cancelAndRefundSubscription(
52
        Request $request,
53
        SubscriptionRepositoryInterface $subscriptionRepository,
54
        SubscriptionManagerInterface $subscriptionManager,
55
        UrlGeneratorInterface $urlGenerator,
56
        Security $security,
57
    ): Response {
58
        try {
59
            /** @var Subscription $subscription */
60
            $subscription = $subscriptionRepository->findById($request->get('id'));
61
        } catch (NoEntityFoundException $e) {
62
            return new RedirectResponse($urlGenerator->generate('parthenon_athena_crud_subscriptions_list'));
63
        }
64
        $subscriptionManager->cancelSubscriptionWithFullRefund($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\Subscr...riptionWithFullRefund(). ( Ignorable by Annotation )

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

64
        $subscriptionManager->cancelSubscriptionWithFullRefund($subscription, /** @scrutinizer ignore-type */ $security->getUser());
Loading history...
65
        $subscriptionRepository->save($subscription);
66
        $request->getSession()->getFlashBag()->add('success', 'Cancelled and refunded');
67
68
        return new RedirectResponse($urlGenerator->generate('parthenon_athena_crud_subscriptions_read', ['id' => $request->get('id')]));
69
    }
70
}
71