Passed
Push — main ( 3919a2...0705b8 )
by Iain
15:39
created

AthenaController::cancelSubscription()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
nc 2
nop 4
dl 0
loc 18
rs 9.9666
c 1
b 0
f 0
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\Component\HttpFoundation\RedirectResponse;
22
use Symfony\Component\HttpFoundation\Request;
23
use Symfony\Component\HttpFoundation\Response;
24
use Symfony\Component\Routing\Annotation\Route;
25
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
26
27
class AthenaController
28
{
29
    #[Route('/athena/billing/subscription/{id}/cancel', name: 'parthenon_billing_athena_subscription_cancel', methods: ['GET'])]
30
    public function cancelSubscription(
31
        Request $request,
32
        SubscriptionRepositoryInterface $subscriptionRepository,
33
        SubscriptionManagerInterface $subscriptionManager,
34
        UrlGeneratorInterface $urlGenerator
35
    ): Response {
36
        try {
37
            /** @var Subscription $subscription */
38
            $subscription = $subscriptionRepository->findById($request->get('id'));
39
        } catch (NoEntityFoundException $e) {
40
            return new RedirectResponse($urlGenerator->generate('parthenon_athena_crud_subscriptions_list'));
41
        }
42
        $subscriptionManager->cancelSubscriptionAtEndOfCurrentPeriod($subscription);
43
        $subscriptionRepository->save($subscription);
44
        $request->getSession()->getFlashBag()->add('success', 'Cancelled');
45
46
        return new RedirectResponse($urlGenerator->generate('parthenon_athena_crud_subscriptions_read', ['id' => $request->get('id')]));
47
    }
48
}
49