Passed
Pull Request — main (#308)
by Paul
18:42 queued 09:10
created

SmsSendChallengeController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
nc 1
nop 2
dl 0
loc 4
rs 10
c 1
b 0
f 0
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\SmsSecondFactorService;
27
use Surfnet\StepupSelfService\SelfServiceBundle\Service\SmsSecondFactorServiceInterface;
28
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
29
use Symfony\Component\HttpFoundation\Request;
30
use Symfony\Component\HttpFoundation\Response;
31
use Symfony\Component\Routing\Attribute\Route;
32
33
class SmsSendChallengeController extends AbstractController
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class SmsSendChallengeController
Loading history...
34
{
35
    public function __construct(
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
36
        private readonly SmsSecondFactorService $smsSecondFactorService,
37
        private readonly ControllerCheckerService $checkerService,
38
    ) {
39
    }
40
    #[Route(
41
        path: '/registration/sms/send-challenge',
42
        name: 'ss_registration_sms_send_challenge',
43
        methods: ['GET','POST'],
44
    )]
45
    public function __invoke(Request $request): Response
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __invoke()
Loading history...
46
    {
47
        $this->checkerService->assertSecondFactorEnabled('sms');
48
49
        $command = new SendSmsChallengeCommand();
50
        $form = $this->createForm(SendSmsChallengeType::class, $command)->handleRequest($request);
51
52
        $otpRequestsRemaining = $this->smsSecondFactorService->getOtpRequestsRemainingCount(
53
            SmsSecondFactorServiceInterface::REGISTRATION_SECOND_FACTOR_ID
54
        );
55
        $maximumOtpRequests = $this->smsSecondFactorService->getMaximumOtpRequestsCount();
56
        $viewVariables = [
57
            'otpRequestsRemaining' => $otpRequestsRemaining,
58
            'maximumOtpRequests' => $maximumOtpRequests,
59
            'verifyEmail' => $this->checkerService->emailVerificationIsRequired(),
60
        ];
61
62
        if ($form->isSubmitted() && $form->isValid()) {
63
            $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

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