SecondFactorRevokeController   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 36
c 2
b 0
f 0
dl 0
loc 61
rs 10
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
B __invoke() 0 53 6
A __construct() 0 4 1
1
<?php
2
3
declare(strict_types = 1);
4
5
/**
6
 * Copyright 2014 SURFnet bv
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *     http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
Coding Style introduced by
Missing @link tag in file comment
Loading history...
20
21
namespace Surfnet\StepupSelfService\SelfServiceBundle\Controller\SecondFactor;
22
23
use LogicException;
24
use Psr\Log\LoggerInterface;
25
use Surfnet\StepupSelfService\SelfServiceBundle\Command\RevokeCommand;
26
use Surfnet\StepupSelfService\SelfServiceBundle\Form\Type\RevokeSecondFactorType;
27
use Surfnet\StepupSelfService\SelfServiceBundle\Service\SecondFactorService;
28
use Symfony\Bridge\Twig\Attribute\Template;
29
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
30
use Symfony\Component\HttpFoundation\Request;
31
use Symfony\Component\HttpFoundation\Response;
32
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
33
use Symfony\Component\Routing\Attribute\Route;
34
35
class SecondFactorRevokeController extends AbstractController
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class SecondFactorRevokeController
Loading history...
36
{
37
    public function __construct(
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
38
        private readonly LoggerInterface $logger,
39
        private readonly SecondFactorService $secondFactorService,
40
    ) {
41
    }
42
43
    #[Template('second_factor/revoke.html.twig')]
44
    #[Route(
45
        path: '/second-factor/{state}/{secondFactorId}/revoke',
46
        name: 'ss_second_factor_revoke',
47
        requirements: ['state' => '^(unverified|verified|vetted)$'],
48
        methods: ['GET','POST']
49
    )]
50
    public function __invoke(Request $request, string $state, string $secondFactorId): array|Response
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __invoke()
Loading history...
51
    {
52
        $identity = $this->getUser()->getIdentity();
0 ignored issues
show
Bug introduced by
The method getIdentity() does not exist on Symfony\Component\Security\Core\User\UserInterface. It seems like you code against a sub-type of Symfony\Component\Security\Core\User\UserInterface such as Surfnet\StepupSelfServic...n\AuthenticatedIdentity. ( Ignorable by Annotation )

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

52
        $identity = $this->getUser()->/** @scrutinizer ignore-call */ getIdentity();
Loading history...
53
54
        if (!$this->secondFactorService->identityHasSecondFactorOfStateWithId($identity->id, $state, $secondFactorId)) {
55
            $this->logger->error(sprintf(
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
56
                'Identity "%s" tried to revoke "%s" second factor "%s", but does not own that second factor',
57
                $identity->id,
58
                $state,
59
                $secondFactorId
60
            ));
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
61
            throw new NotFoundHttpException();
62
        }
63
64
        $secondFactor = match ($state) {
65
            'unverified' => $this->secondFactorService->findOneUnverified($secondFactorId),
66
            'verified' => $this->secondFactorService->findOneVerified($secondFactorId),
67
            'vetted' => $this->secondFactorService->findOneVetted($secondFactorId),
68
            default => throw new LogicException('There are no other types of second factor.'),
69
        };
70
71
        if ($secondFactor === null) {
72
            throw new NotFoundHttpException(
73
                sprintf("No %s second factor with id '%s' exists.", $state, $secondFactorId)
74
            );
75
        }
76
77
        $command = new RevokeCommand();
78
        $command->identity = $identity;
79
        $command->secondFactor = $secondFactor;
80
81
        $form = $this->createForm(RevokeSecondFactorType::class, $command)->handleRequest($request);
82
83
        if ($form->isSubmitted() && $form->isValid()) {
84
            if ($this->secondFactorService->revoke($command)) {
85
                $this->addFlash('success', 'ss.second_factor.revoke.alert.revocation_successful');
86
            } else {
87
                $this->addFlash('error', 'ss.second_factor.revoke.alert.revocation_failed');
88
            }
89
90
            return $this->redirectToRoute('ss_second_factor_list');
91
        }
92
93
        return [
94
            'form'         => $form->createView(),
95
            'secondFactor' => $secondFactor,
96
        ];
97
    }
98
}
99