Passed
Pull Request — main (#308)
by Paul
20:03 queued 12:40
created

SmsController   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 66
c 2
b 0
f 0
dl 0
loc 111
rs 10
wmc 15

3 Methods

Rating   Name   Duplication   Size   Complexity  
A sendChallenge() 0 42 5
A __construct() 0 6 1
B provePossession() 0 57 9
1
<?php
2
3
declare(strict_types = 1);
4
5
/**
6
 * Copyright 2014 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\Registration;
22
23
use Psr\Log\LoggerInterface;
24
use Surfnet\StepupSelfService\SelfServiceBundle\Service\InstitutionConfigurationOptionsService;
25
use Symfony\Bridge\Twig\Attribute\Template;
26
use Surfnet\StepupSelfService\SelfServiceBundle\Command\SendSmsChallengeCommand;
27
use Surfnet\StepupSelfService\SelfServiceBundle\Command\VerifySmsChallengeCommand;
28
use Surfnet\StepupSelfService\SelfServiceBundle\Controller\Controller;
29
use Surfnet\StepupSelfService\SelfServiceBundle\Form\Type\SendSmsChallengeType;
30
use Surfnet\StepupSelfService\SelfServiceBundle\Form\Type\VerifySmsChallengeType;
31
use Surfnet\StepupSelfService\SelfServiceBundle\Service\SmsSecondFactorService;
32
use Surfnet\StepupSelfService\SelfServiceBundle\Service\SmsSecondFactorServiceInterface;
33
use Symfony\Component\HttpFoundation\RedirectResponse;
34
use Symfony\Component\HttpFoundation\Request;
35
use Symfony\Component\Routing\Annotation\Route;
36
37
class SmsController extends Controller
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class SmsController
Loading history...
38
{
39
    public function __construct(
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
40
        LoggerInterface                         $logger,
41
        InstitutionConfigurationOptionsService  $configurationOptionsService,
42
        private readonly SmsSecondFactorService $smsSecondFactorService,
43
    ) {
44
        parent::__construct($logger, $configurationOptionsService);
45
    }
46
47
    #[Template('registration/sms/send_challenge.html.twig')]
48
    #[Route(
49
        path: '/registration/sms/send-challenge',
50
        name: 'ss_registration_sms_send_challenge',
51
        methods: ['GET','POST'],
52
    )]
53
    public function sendChallenge(Request $request): array|RedirectResponse
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function sendChallenge()
Loading history...
54
    {
55
        $this->assertSecondFactorEnabled('sms');
56
57
        $identity = $this->getIdentity();
58
59
        $command = new SendSmsChallengeCommand();
60
        $form = $this->createForm(SendSmsChallengeType::class, $command)->handleRequest($request);
61
62
        $otpRequestsRemaining = $this->smsSecondFactorService->getOtpRequestsRemainingCount(
63
            SmsSecondFactorServiceInterface::REGISTRATION_SECOND_FACTOR_ID
64
        );
65
        $maximumOtpRequests = $this->smsSecondFactorService->getMaximumOtpRequestsCount();
66
        $viewVariables = [
67
            'otpRequestsRemaining' => $otpRequestsRemaining,
68
            'maximumOtpRequests' => $maximumOtpRequests,
69
            'verifyEmail' => $this->emailVerificationIsRequired(),
70
        ];
71
72
        if ($form->isSubmitted() && $form->isValid()) {
73
            $command->identity = $identity->id;
74
            $command->institution = $identity->institution;
75
76
            if ($otpRequestsRemaining === 0) {
77
                $this->addFlash('error', 'ss.prove_phone_possession.challenge_request_limit_reached');
78
                return ['form' => $form->createView(), ...$viewVariables];
79
            }
80
81
            if ($this->smsSecondFactorService->sendChallenge($command)) {
82
                return $this->redirect($this->generateUrl('ss_registration_sms_prove_possession'));
83
            } else {
84
                $this->addFlash('error', 'ss.prove_phone_possession.send_sms_challenge_failed');
85
            }
86
        }
87
88
        return ['form' => $form->createView(), ...$viewVariables];
89
    }
90
91
    #[Template('registration/sms/prove_possession.html.twig')]
92
    #[Route(
93
        path: '/registration/sms/prove-possession',
94
        name: 'ss_registration_sms_prove_possession',
95
        methods: ['GET','POST'],
96
    )]
97
    public function provePossession(Request $request): RedirectResponse|array
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function provePossession()
Loading history...
98
    {
99
        $this->assertSecondFactorEnabled('sms');
100
101
        /** @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...
102
        $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

102
        /** @scrutinizer ignore-call */ 
103
        $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...
103
104
        if (!$service->hasSmsVerificationState(SmsSecondFactorServiceInterface::REGISTRATION_SECOND_FACTOR_ID)) {
105
            $this->get('session')->getFlashBag()->add('notice', 'ss.registration.sms.alert.no_verification_state');
106
107
            return $this->redirectToRoute('ss_registration_sms_send_challenge');
108
        }
109
110
        $identity = $this->getIdentity();
111
112
        $command = new VerifySmsChallengeCommand();
113
        $command->identity = $identity->id;
114
115
        $form = $this->createForm(VerifySmsChallengeType::class, $command)->handleRequest($request);
116
117
        if ($form->isSubmitted() && $form->isValid()) {
118
            $result = $service->provePossession($command);
119
120
            if ($result->isSuccessful()) {
121
                $service->clearSmsVerificationState(SmsSecondFactorServiceInterface::REGISTRATION_SECOND_FACTOR_ID);
122
123
                if ($this->emailVerificationIsRequired()) {
124
                    return $this->redirectToRoute(
125
                        'ss_registration_email_verification_email_sent',
126
                        ['secondFactorId' => $result->getSecondFactorId()]
127
                    );
128
                } else {
129
                    return $this->redirectToRoute(
130
                        'ss_second_factor_vetting_types',
131
                        ['secondFactorId' => $result->getSecondFactorId()]
132
                    );
133
                }
134
            } elseif ($result->wasIncorrectChallengeResponseGiven()) {
135
                $this->addFlash('error', 'ss.prove_phone_possession.incorrect_challenge_response');
136
            } elseif ($result->hasChallengeExpired()) {
137
                $this->addFlash('error', 'ss.prove_phone_possession.challenge_expired');
138
            } elseif ($result->wereTooManyAttemptsMade()) {
139
                $this->addFlash('error', 'ss.prove_phone_possession.too_many_attempts');
140
            } else {
141
                $this->addFlash('error', 'ss.prove_phone_possession.proof_of_possession_failed');
142
            }
143
        }
144
145
        return [
146
            'form' => $form->createView(),
147
            'verifyEmail' => $this->emailVerificationIsRequired(),
148
        ];
149
    }
150
}
151