Passed
Pull Request — main (#308)
by Paul
16:02 queued 07:12
created

SmsController   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 65
dl 0
loc 106
rs 10
c 1
b 0
f 0
wmc 14

2 Methods

Rating   Name   Duplication   Size   Complexity  
A sendChallenge() 0 42 5
B provePossession() 0 57 9
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 Symfony\Bridge\Twig\Attribute\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
    #[Template('registration/sms/send_challenge.html.twig')]
35
    #[Route(
36
        path: '/registration/sms/send-challenge',
37
        name: 'ss_registration_sms_send_challenge',
38
        methods: ['GET','POST'],
39
    )]
40
    public function sendChallenge(Request $request): array|\Symfony\Component\HttpFoundation\RedirectResponse
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function sendChallenge()
Loading history...
41
    {
42
        $this->assertSecondFactorEnabled('sms');
43
44
        $identity = $this->getIdentity();
45
46
        $command = new SendSmsChallengeCommand();
47
        $form = $this->createForm(SendSmsChallengeType::class, $command)->handleRequest($request);
48
49
        /** @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...
50
        $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

50
        /** @scrutinizer ignore-call */ 
51
        $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...
51
        $otpRequestsRemaining = $service->getOtpRequestsRemainingCount(SmsSecondFactorServiceInterface::REGISTRATION_SECOND_FACTOR_ID);
52
        $maximumOtpRequests = $service->getMaximumOtpRequestsCount();
53
        $viewVariables = [
54
            'otpRequestsRemaining' => $otpRequestsRemaining,
55
            'maximumOtpRequests' => $maximumOtpRequests,
56
            'verifyEmail' => $this->emailVerificationIsRequired(),
57
        ];
58
59
        if ($form->isSubmitted() && $form->isValid()) {
60
            $command->identity = $identity->id;
61
            $command->institution = $identity->institution;
62
63
            if ($otpRequestsRemaining === 0) {
64
                $this->addFlash('error', 'ss.prove_phone_possession.challenge_request_limit_reached');
65
                return ['form' => $form->createView(), ...$viewVariables];
66
            }
67
68
            if ($service->sendChallenge($command)) {
69
                return $this->redirect($this->generateUrl('ss_registration_sms_prove_possession'));
70
            } else {
71
                $this->addFlash('error', 'ss.prove_phone_possession.send_sms_challenge_failed');
72
            }
73
        }
74
75
        return ['form' => $form->createView(), ...$viewVariables];
76
    }
77
78
    /**
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...
79
     * @return array|\Symfony\Component\HttpFoundation\RedirectResponse
80
     */
81
    #[Template('registration/sms/prove_possession.html.twig')]
82
    #[Route(
83
        path: '/registration/sms/prove-possession',
84
        name: 'ss_registration_sms_prove_possession',
85
        methods: ['GET','POST'],
86
    )]
87
    public function provePossession(Request $request): \Symfony\Component\HttpFoundation\RedirectResponse|array
88
    {
89
        $this->assertSecondFactorEnabled('sms');
90
91
        /** @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...
92
        $service = $this->get('surfnet_stepup_self_service_self_service.service.sms_second_factor');
93
94
        if (!$service->hasSmsVerificationState(SmsSecondFactorServiceInterface::REGISTRATION_SECOND_FACTOR_ID)) {
95
            $this->get('session')->getFlashBag()->add('notice', 'ss.registration.sms.alert.no_verification_state');
96
97
            return $this->redirectToRoute('ss_registration_sms_send_challenge');
98
        }
99
100
        $identity = $this->getIdentity();
101
102
        $command = new VerifySmsChallengeCommand();
103
        $command->identity = $identity->id;
104
105
        $form = $this->createForm(VerifySmsChallengeType::class, $command)->handleRequest($request);
106
107
        if ($form->isSubmitted() && $form->isValid()) {
108
            $result = $service->provePossession($command);
109
110
            if ($result->isSuccessful()) {
111
                $service->clearSmsVerificationState(SmsSecondFactorServiceInterface::REGISTRATION_SECOND_FACTOR_ID);
112
113
                if ($this->emailVerificationIsRequired()) {
114
                    return $this->redirectToRoute(
115
                        'ss_registration_email_verification_email_sent',
116
                        ['secondFactorId' => $result->getSecondFactorId()]
117
                    );
118
                } else {
119
                    return $this->redirectToRoute(
120
                        'ss_second_factor_vetting_types',
121
                        ['secondFactorId' => $result->getSecondFactorId()]
122
                    );
123
                }
124
            } elseif ($result->wasIncorrectChallengeResponseGiven()) {
125
                $this->addFlash('error', 'ss.prove_phone_possession.incorrect_challenge_response');
126
            } elseif ($result->hasChallengeExpired()) {
127
                $this->addFlash('error', 'ss.prove_phone_possession.challenge_expired');
128
            } elseif ($result->wereTooManyAttemptsMade()) {
129
                $this->addFlash('error', 'ss.prove_phone_possession.too_many_attempts');
130
            } else {
131
                $this->addFlash('error', 'ss.prove_phone_possession.proof_of_possession_failed');
132
            }
133
        }
134
135
        return [
136
            'form' => $form->createView(),
137
            'verifyEmail' => $this->emailVerificationIsRequired(),
138
        ];
139
    }
140
}
141