YubikeyController   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 58
Duplicated Lines 39.66 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 6
dl 23
loc 58
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getVettingService() 0 4 1
B verifyAction() 23 41 7

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
/**
4
 * Copyright 2014 SURFnet bv
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     http://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
19
namespace Surfnet\StepupRa\RaBundle\Controller\Vetting;
20
21
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
22
use Surfnet\StepupRa\RaBundle\Command\VerifyYubikeyPublicIdCommand;
23
use Surfnet\StepupRa\RaBundle\Form\Type\VerifyYubikeyPublicIdType;
24
use Surfnet\StepupRa\RaBundle\Service\VettingService;
25
use Symfony\Component\Form\FormError;
26
use Symfony\Component\HttpFoundation\Request;
27
use Symfony\Component\HttpFoundation\Response;
28
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
29
30
class YubikeyController extends SecondFactorController
31
{
32
    /**
33
     * @Template
34
     * @param Request $request
35
     * @param string  $procedureId
36
     * @return array|Response
37
     */
38
    public function verifyAction(Request $request, $procedureId)
39
    {
40
        $this->assertSecondFactorEnabled('yubikey');
41
42
        $this->denyAccessUnlessGranted(['ROLE_RA']);
43
44
        $logger = $this->get('ra.procedure_logger')->forProcedure($procedureId);
45
        $logger->notice('Requested Yubikey Verfication');
46
47 View Code Duplication
        if (!$this->getVettingService()->hasProcedure($procedureId)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
48
            $logger->notice(sprintf('Vetting procedure "%s" not found', $procedureId));
49
            throw new NotFoundHttpException(sprintf('Vetting procedure "%s" not found', $procedureId));
50
        }
51
52
        $command = new VerifyYubikeyPublicIdCommand();
53
        $form = $this->createForm(VerifyYubikeyPublicIdType::class, $command)->handleRequest($request);
54
55 View Code Duplication
        if ($form->isSubmitted() && $form->isValid()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
56
            $result = $this->getVettingService()->verifyYubikeyPublicId($procedureId, $command);
57
58
            if ($result->didPublicIdMatch()) {
59
                $logger->notice('Yubikey Verified, redirecting to verify identity');
60
61
                return $this->redirectToRoute('ra_vetting_verify_identity', ['procedureId' => $procedureId]);
62
            }
63
64
            if ($result->wasOtpInvalid()) {
65
                $this->addFlash('error', 'ra.verify_yubikey_command.otp.otp_invalid');
66
            } elseif ($result->didOtpVerificationFail()) {
67
                $this->addFlash('error', 'ra.verify_yubikey_command.otp.verification_error');
68
            } else {
69
                $this->addFlash('error', 'ra.prove_yubikey_possession.different_yubikey_used');
70
            }
71
72
            $logger->notice('Yubikey could not be verified, added error to form');
73
        }
74
75
        $logger->notice('Rendering Yubikey Verification Form');
76
        // OTP field is rendered empty in the template.
77
        return ['form' => $form->createView()];
78
    }
79
80
    /**
81
     * @return VettingService
82
     */
83
    private function getVettingService()
84
    {
85
        return $this->get('ra.service.vetting');
86
    }
87
}
88