Passed
Push — feature/build-and-publish-test... ( c594c2...5abced )
by
unknown
18:44
created

SmsController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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

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