SmsSendChallengeController   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 30
dl 0
loc 51
rs 10
c 2
b 0
f 0
wmc 6

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __invoke() 0 44 5
1
<?php
2
3
declare(strict_types = 1);
4
5
/**
6
 * Copyright 2023 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\Sms;
22
23
use Surfnet\StepupSelfService\SelfServiceBundle\Command\SendSmsChallengeCommand;
24
use Surfnet\StepupSelfService\SelfServiceBundle\Form\Type\SendSmsChallengeType;
25
use Surfnet\StepupSelfService\SelfServiceBundle\Service\ControllerCheckerService;
26
use Surfnet\StepupSelfService\SelfServiceBundle\Service\SmsSecondFactorServiceInterface;
27
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
28
use Symfony\Component\HttpFoundation\Request;
29
use Symfony\Component\HttpFoundation\Response;
30
use Symfony\Component\Routing\Attribute\Route;
31
32
class SmsSendChallengeController extends AbstractController
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class SmsSendChallengeController
Loading history...
33
{
34
    public function __construct(
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
35
        private readonly SmsSecondFactorServiceInterface $smsSecondFactorService,
36
        private readonly ControllerCheckerService $checkerService,
37
    ) {
38
    }
39
    #[Route(
40
        path: '/registration/sms/send-challenge',
41
        name: 'ss_registration_sms_send_challenge',
42
        methods: ['GET','POST'],
43
    )]
44
    public function __invoke(Request $request): Response
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __invoke()
Loading history...
45
    {
46
        $this->checkerService->assertSecondFactorEnabled('sms');
47
48
        $command = new SendSmsChallengeCommand();
49
        $form = $this->createForm(SendSmsChallengeType::class, $command)->handleRequest($request);
50
51
        $otpRequestsRemaining = $this->smsSecondFactorService->getOtpRequestsRemainingCount(
52
            SmsSecondFactorServiceInterface::REGISTRATION_SECOND_FACTOR_ID
53
        );
54
        $maximumOtpRequests = $this->smsSecondFactorService->getMaximumOtpRequestsCount();
55
        $viewVariables = [
56
            'otpRequestsRemaining' => $otpRequestsRemaining,
57
            'maximumOtpRequests' => $maximumOtpRequests,
58
            'verifyEmail' => $this->checkerService->emailVerificationIsRequired(),
59
        ];
60
61
        if ($form->isSubmitted() && $form->isValid()) {
62
            $command->identity = $this->getUser()->getIdentity()->id;
0 ignored issues
show
Bug introduced by
The method getIdentity() does not exist on Symfony\Component\Security\Core\User\UserInterface. It seems like you code against a sub-type of Symfony\Component\Security\Core\User\UserInterface such as Surfnet\StepupSelfServic...n\AuthenticatedIdentity. ( Ignorable by Annotation )

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

62
            $command->identity = $this->getUser()->/** @scrutinizer ignore-call */ getIdentity()->id;
Loading history...
63
            $command->institution = $this->getUser()->getIdentity()->institution;
64
65
            if ($otpRequestsRemaining === 0) {
66
                $this->addFlash('error', 'ss.prove_phone_possession.challenge_request_limit_reached');
67
                return $this->render(
68
                    'registration/sms/send_challenge.html.twig',
69
                    ['form' => $form->createView(), ...$viewVariables]
70
                );
71
            }
72
73
            if ($this->smsSecondFactorService->sendChallenge($command)) {
74
                return $this->redirect($this->generateUrl('ss_registration_sms_prove_possession'));
75
            } else {
76
                $this->addFlash('error', 'ss.prove_phone_possession.send_sms_challenge_failed');
77
            }
78
        }
79
80
        return $this->render(
81
            'registration/sms/send_challenge.html.twig',
82
            ['form' => $form->createView(), ...$viewVariables]
83
        );
84
    }
85
}
86