Passed
Push — feature/symfony6-upgrade ( 35a069...81bcc7 )
by Paul
05:48
created

SmsSendChallengeController   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 54
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 31
c 1
b 0
f 0
dl 0
loc 54
rs 10
wmc 6

2 Methods

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