Passed
Push — feature/symfony6-upgrade ( 166417...86e2b9 )
by Paul
06:45
created

SmsController::provePossessionAction()   B

Complexity

Conditions 9
Paths 8

Size

Total Lines 56
Code Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 36
c 0
b 0
f 0
nc 8
nop 1
dl 0
loc 56
rs 8.0555

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
/**
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
 */
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...
18
19
namespace Surfnet\StepupSelfService\SelfServiceBundle\Controller\Registration;
20
21
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
22
use Surfnet\StepupSelfService\SelfServiceBundle\Command\SendSmsChallengeCommand;
23
use Surfnet\StepupSelfService\SelfServiceBundle\Command\VerifySmsChallengeCommand;
24
use Surfnet\StepupSelfService\SelfServiceBundle\Controller\Controller;
25
use Surfnet\StepupSelfService\SelfServiceBundle\Form\Type\SendSmsChallengeType;
26
use Surfnet\StepupSelfService\SelfServiceBundle\Form\Type\VerifySmsChallengeType;
27
use Surfnet\StepupSelfService\SelfServiceBundle\Service\SmsSecondFactorService;
28
use Surfnet\StepupSelfService\SelfServiceBundle\Service\SmsSecondFactorServiceInterface;
29
use Symfony\Component\HttpFoundation\Request;
30
use Symfony\Component\Routing\Annotation\Route;
31
32
class SmsController extends Controller
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class SmsController
Loading history...
33
{
34
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $request should have a doc-comment as per coding-style.
Loading history...
35
     * @Template
36
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
37
    #[Route(
38
        path: '/registration/sms/send-challenge',
39
        name: 'ss_registration_sms_send_challenge',
40
        methods: ['GET','POST'],
41
    )]
42
    public function sendChallenge(Request $request): array|\Symfony\Component\HttpFoundation\RedirectResponse
43
    {
44
        $this->assertSecondFactorEnabled('sms');
45
46
        $identity = $this->getIdentity();
47
48
        $command = new SendSmsChallengeCommand();
49
        $form = $this->createForm(SendSmsChallengeType::class, $command)->handleRequest($request);
50
51
        /** @var SmsSecondFactorService $service */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
52
        $service = $this->get('surfnet_stepup_self_service_self_service.service.sms_second_factor');
0 ignored issues
show
Bug introduced by
The method get() does not exist on Surfnet\StepupSelfServic...istration\SmsController. ( Ignorable by Annotation )

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

52
        /** @scrutinizer ignore-call */ 
53
        $service = $this->get('surfnet_stepup_self_service_self_service.service.sms_second_factor');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
53
        $otpRequestsRemaining = $service->getOtpRequestsRemainingCount(SmsSecondFactorServiceInterface::REGISTRATION_SECOND_FACTOR_ID);
54
        $maximumOtpRequests = $service->getMaximumOtpRequestsCount();
55
        $viewVariables = [
56
            'otpRequestsRemaining' => $otpRequestsRemaining,
57
            'maximumOtpRequests' => $maximumOtpRequests,
58
            'verifyEmail' => $this->emailVerificationIsRequired(),
59
        ];
60
61
        if ($form->isSubmitted() && $form->isValid()) {
62
            $command->identity = $identity->id;
63
            $command->institution = $identity->institution;
64
65
            if ($otpRequestsRemaining === 0) {
66
                $this->addFlash('error', 'ss.prove_phone_possession.challenge_request_limit_reached');
67
                return ['form' => $form->createView(), ...$viewVariables];
68
            }
69
70
            if ($service->sendChallenge($command)) {
71
                return $this->redirect($this->generateUrl('ss_registration_sms_prove_possession'));
72
            } else {
73
                $this->addFlash('error', 'ss.prove_phone_possession.send_sms_challenge_failed');
74
            }
75
        }
76
77
        return ['form' => $form->createView(), ...$viewVariables];
78
    }
79
80
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $request should have a doc-comment as per coding-style.
Loading history...
81
     * @Template
82
     * @return array|\Symfony\Component\HttpFoundation\RedirectResponse
0 ignored issues
show
Coding Style introduced by
Tag value for @return tag indented incorrectly; expected 3 spaces but found 1
Loading history...
83
     */
84
    #[Route(
85
        path: '/registration/sms/prove-possession',
86
        name: 'ss_registration_sms_prove_possession',
87
        methods: ['GET','POST'],
88
    )]
89
    public function provePossession(Request $request): \Symfony\Component\HttpFoundation\RedirectResponse|array
90
    {
91
        $this->assertSecondFactorEnabled('sms');
92
93
        /** @var SmsSecondFactorService $service */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
94
        $service = $this->get('surfnet_stepup_self_service_self_service.service.sms_second_factor');
95
96
        if (!$service->hasSmsVerificationState(SmsSecondFactorServiceInterface::REGISTRATION_SECOND_FACTOR_ID)) {
97
            $this->get('session')->getFlashBag()->add('notice', 'ss.registration.sms.alert.no_verification_state');
98
99
            return $this->redirectToRoute('ss_registration_sms_send_challenge');
100
        }
101
102
        $identity = $this->getIdentity();
103
104
        $command = new VerifySmsChallengeCommand();
105
        $command->identity = $identity->id;
106
107
        $form = $this->createForm(VerifySmsChallengeType::class, $command)->handleRequest($request);
108
109
        if ($form->isSubmitted() && $form->isValid()) {
110
            $result = $service->provePossession($command);
111
112
            if ($result->isSuccessful()) {
113
                $service->clearSmsVerificationState(SmsSecondFactorServiceInterface::REGISTRATION_SECOND_FACTOR_ID);
114
115
                if ($this->emailVerificationIsRequired()) {
116
                    return $this->redirectToRoute(
117
                        'ss_registration_email_verification_email_sent',
118
                        ['secondFactorId' => $result->getSecondFactorId()]
119
                    );
120
                } else {
121
                    return $this->redirectToRoute(
122
                        'ss_second_factor_vetting_types',
123
                        ['secondFactorId' => $result->getSecondFactorId()]
124
                    );
125
                }
126
            } elseif ($result->wasIncorrectChallengeResponseGiven()) {
127
                $this->addFlash('error', 'ss.prove_phone_possession.incorrect_challenge_response');
128
            } elseif ($result->hasChallengeExpired()) {
129
                $this->addFlash('error', 'ss.prove_phone_possession.challenge_expired');
130
            } elseif ($result->wereTooManyAttemptsMade()) {
131
                $this->addFlash('error', 'ss.prove_phone_possession.too_many_attempts');
132
            } else {
133
                $this->addFlash('error', 'ss.prove_phone_possession.proof_of_possession_failed');
134
            }
135
        }
136
137
        return [
138
            'form' => $form->createView(),
139
            'verifyEmail' => $this->emailVerificationIsRequired(),
140
        ];
141
    }
142
}
143