Passed
Pull Request — main (#308)
by Paul
12:52 queued 05:50
created

SecondFactorRevokeController::__invoke()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 54
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 35
nc 5
nop 3
dl 0
loc 54
rs 8.7377
c 1
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types = 1);
4
5
/**
6
 * Copyright 2023 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\Controller\Controller;
27
use Surfnet\StepupSelfService\SelfServiceBundle\Form\Type\RevokeSecondFactorType;
28
use Surfnet\StepupSelfService\SelfServiceBundle\Service\InstitutionConfigurationOptionsService;
29
use Surfnet\StepupSelfService\SelfServiceBundle\Service\SecondFactorService;
30
use Symfony\Bridge\Twig\Attribute\Template;
31
use Symfony\Component\HttpFoundation\Request;
32
use Symfony\Component\HttpFoundation\Response;
33
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
34
use Symfony\Component\Routing\Attribute\Route;
35
36
class SecondFactorRevokeController extends Controller
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class SecondFactorRevokeController
Loading history...
37
{
38
    public function __construct(
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
39
        private readonly LoggerInterface $logger,
40
        InstitutionConfigurationOptionsService $configurationOptionsService,
41
        private readonly SecondFactorService $secondFactorService,
42
0 ignored issues
show
Coding Style introduced by
Blank lines are not allowed in a multi-line function declaration
Loading history...
43
    ) {
44
        parent::__construct($logger, $configurationOptionsService);
45
    }
46
47
    #[Template('second_factor/revoke.html.twig')]
48
    #[Route(
49
        path: '/second-factor/{state}/{secondFactorId}/revoke',
50
        name: 'ss_second_factor_revoke',
51
        requirements: ['state' => '^(unverified|verified|vetted)$'],
52
        methods: ['GET','POST']
53
    )]
54
    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...
55
    {
56
        $identity = $this->getIdentity();
57
58
        if (!$this->secondFactorService->identityHasSecondFactorOfStateWithId($identity->id, $state, $secondFactorId)) {
0 ignored issues
show
Bug introduced by
It seems like $identity->id can also be of type null; however, parameter $identityId of Surfnet\StepupSelfServic...ndFactorOfStateWithId() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

58
        if (!$this->secondFactorService->identityHasSecondFactorOfStateWithId(/** @scrutinizer ignore-type */ $identity->id, $state, $secondFactorId)) {
Loading history...
59
            $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...
60
                'Identity "%s" tried to revoke "%s" second factor "%s", but does not own that second factor',
61
                $identity->id,
62
                $state,
63
                $secondFactorId
64
            ));
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...
65
            throw new NotFoundHttpException();
66
        }
67
68
        $secondFactor = match ($state) {
69
            'unverified' => $this->secondFactorService->findOneUnverified($secondFactorId),
70
            'verified' => $this->secondFactorService->findOneVerified($secondFactorId),
71
            'vetted' => $this->secondFactorService->findOneVetted($secondFactorId),
72
            default => throw new LogicException('There are no other types of second factor.'),
73
        };
74
75
        if ($secondFactor === null) {
76
            throw new NotFoundHttpException(
77
                sprintf("No %s second factor with id '%s' exists.", $state, $secondFactorId)
78
            );
79
        }
80
81
        $command = new RevokeCommand();
82
        $command->identity = $identity;
83
        $command->secondFactor = $secondFactor;
84
85
        $form = $this->createForm(RevokeSecondFactorType::class, $command)->handleRequest($request);
86
87
        if ($form->isSubmitted() && $form->isValid()) {
88
            
89
            if ($this->secondFactorService->revoke($command)) {
90
                $this->addFlash('success', 'ss.second_factor.revoke.alert.revocation_successful');
91
            } else {
92
                $this->addFlash('error', 'ss.second_factor.revoke.alert.revocation_failed');
93
            }
94
95
            return $this->redirectToRoute('ss_second_factor_list');
96
        }
97
98
        return [
99
            'form'         => $form->createView(),
100
            'secondFactor' => $secondFactor,
101
        ];
102
    }
103
}
104