Completed
Push — master ( 990ac1...53601d )
by
unknown
02:13
created

SmsController::provePossessionAction()   B

Complexity

Conditions 8
Paths 8

Size

Total Lines 53
Code Lines 33

Duplication

Lines 11
Ratio 20.75 %

Importance

Changes 0
Metric Value
dl 11
loc 53
rs 7.1199
c 0
b 0
f 0
cc 8
eloc 33
nc 8
nop 1

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
 */
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\Service\SmsSecondFactorService;
26
use Symfony\Component\Form\FormError;
27
use Symfony\Component\HttpFoundation\Request;
28
29
class SmsController extends Controller
30
{
31
    /**
32
     * @Template
33
     */
34
    public function sendChallengeAction(Request $request)
35
    {
36
        $this->assertSecondFactorEnabled('sms');
37
38
        $identity = $this->getIdentity();
39
40
        $command = new SendSmsChallengeCommand();
41
        $form = $this->createForm('ss_send_sms_challenge', $command)->handleRequest($request);
42
43
        /** @var SmsSecondFactorService $service */
44
        $service = $this->get('surfnet_stepup_self_service_self_service.service.sms_second_factor');
45
        $otpRequestsRemaining = $service->getOtpRequestsRemainingCount();
46
        $maximumOtpRequests = $service->getMaximumOtpRequestsCount();
47
        $viewVariables = ['otpRequestsRemaining' => $otpRequestsRemaining, 'maximumOtpRequests' => $maximumOtpRequests];
48
49
        if ($form->isValid()) {
50
            $command->identity = $identity->id;
51
            $command->institution = $identity->institution;
52
53
            if ($otpRequestsRemaining === 0) {
54
                $form->addError(new FormError('ss.prove_phone_possession.challenge_request_limit_reached'));
55
56
                return array_merge(['form' => $form->createView()], $viewVariables);
57
            }
58
59
            if ($service->sendChallenge($command)) {
60
                return $this->redirect($this->generateUrl('ss_registration_sms_prove_possession'));
61
            } else {
62
                $form->addError(new FormError('ss.prove_phone_possession.send_sms_challenge_failed'));
63
            }
64
        }
65
66
        return array_merge(
67
            [
68
                'form' => $form->createView(),
69
                'verifyEmail' => $this->emailVerificationIsRequired(),
70
            ],
71
            $viewVariables
72
        );
73
    }
74
75
    /**
76
     * @Template
77
     * @param Request $request
78
     * @return array|\Symfony\Component\HttpFoundation\RedirectResponse
79
     */
80
    public function provePossessionAction(Request $request)
81
    {
82
        $this->assertSecondFactorEnabled('sms');
83
84
        /** @var SmsSecondFactorService $service */
85
        $service = $this->get('surfnet_stepup_self_service_self_service.service.sms_second_factor');
86
87
        if (!$service->hasSmsVerificationState()) {
88
            $this->get('session')->getFlashBag()->add('notice', 'ss.registration.sms.alert.no_verification_state');
89
90
            return $this->redirectToRoute('ss_registration_sms_send_challenge');
91
        }
92
93
        $identity = $this->getIdentity();
94
95
        $command = new VerifySmsChallengeCommand();
96
        $command->identity = $identity->id;
97
98
        $form = $this->createForm('ss_verify_sms_challenge', $command)->handleRequest($request);
99
100
        if ($form->isValid()) {
101
            $result = $service->provePossession($command);
102
103
            if ($result->isSuccessful()) {
104
                $service->clearSmsVerificationState();
105
106 View Code Duplication
                if ($this->emailVerificationIsRequired()) {
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...
107
                    return $this->redirectToRoute(
108
                        'ss_registration_email_verification_email_sent',
109
                        ['secondFactorId' => $result->getSecondFactorId()]
110
                    );
111
                } else {
112
                    return $this->redirectToRoute(
113
                        'ss_registration_registration_email_sent',
114
                        ['secondFactorId' => $result->getSecondFactorId()]
115
                    );
116
                }
117
            } elseif ($result->wasIncorrectChallengeResponseGiven()) {
118
                $form->addError(new FormError('ss.prove_phone_possession.incorrect_challenge_response'));
119
            } elseif ($result->hasChallengeExpired()) {
120
                $form->addError(new FormError('ss.prove_phone_possession.challenge_expired'));
121
            } elseif ($result->wereTooManyAttemptsMade()) {
122
                $form->addError(new FormError('ss.prove_phone_possession.too_many_attempts'));
123
            } else {
124
                $form->addError(new FormError('ss.prove_phone_possession.proof_of_possession_failed'));
125
            }
126
        }
127
128
        return [
129
            'form' => $form->createView(),
130
            'verifyEmail' => $this->emailVerificationIsRequired(),
131
        ];
132
    }
133
}
134