Completed
Push — master ( 22f213...24f6ce )
by Guilherme
17:25
created

VerificationController::getNextResendDate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of the login-cidadao project or it's bundles.
4
 *
5
 * (c) Guilherme Donato <guilhermednt on github>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LoginCidadao\PhoneVerificationBundle\Controller;
12
13
use LoginCidadao\PhoneVerificationBundle\Exception\VerificationNotSentException;
14
use LoginCidadao\PhoneVerificationBundle\Model\PhoneVerificationInterface;
15
use LoginCidadao\PhoneVerificationBundle\Service\PhoneVerificationServiceInterface;
16
use LoginCidadao\TaskStackBundle\Service\TaskStackManagerInterface;
17
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
18
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
19
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
20
use Symfony\Component\Form\FormError;
21
use Symfony\Component\HttpFoundation\Request;
22
use Symfony\Component\HttpKernel\Exception\TooManyRequestsHttpException;
23
use Symfony\Component\Translation\TranslatorInterface;
24
25
/**
26
 * @codeCoverageIgnore
27
 */
28
class VerificationController extends Controller
29
{
30
    /**
31
     * @Route("/task/verify-phone/{id}", name="lc_verify_phone")
32
     * @Template()
33
     */
34
    public function verifyAction(Request $request, $id)
35
    {
36
        /** @var PhoneVerificationServiceInterface $phoneVerificationService */
37
        $phoneVerificationService = $this->get('phone_verification');
38
39
        /** @var PhoneVerificationInterface $pendingVerifications */
40
        $verification = $phoneVerificationService->getPendingPhoneVerificationById($id, $this->getUser());
41
42
        if (!$verification) {
43
            return $this->noVerificationOrVerified($request);
44
        }
45
46
        $form = $this->createForm('LoginCidadao\PhoneVerificationBundle\Form\PhoneVerificationType');
47
        $form->handleRequest($request);
48
        $verified = false;
49
50
        if ($form->isValid()) {
51
            $code = $form->getData()['verificationCode'];
52
            $verified = $phoneVerificationService->verify($verification, $code);
53
            if (!$verified) {
54
                $form->get('verificationCode')->addError(new FormError(
55
                    $this->get('translator')->trans('tasks.verify_phone.form.errors.verificationCode.invalid_code')
56
                ));
57
            }
58
        }
59
60
        if (!$verification || $verified) {
61
            return $this->noVerificationOrVerified($request);
62
        }
63
64
        $nextResend = $this->getNextResendDate($verification);
65
66
        return ['verification' => $verification, 'nextResend' => $nextResend, 'form' => $form->createView()];
67
    }
68
69
    /**
70
     * @Route("/task/verify-phone/{id}/success", name="lc_phone_verification_success")
71
     * @Template("LoginCidadaoPhoneVerificationBundle:Verification:response.html.twig")
72
     */
73
    public function successAction(Request $request, $id)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
74
    {
75
        /** @var TranslatorInterface $translator */
76
        $translator = $this->get('translator');
77
78
        return [
79
            'message' => $translator->trans('tasks.verify_phone.success'),
80
            'target' => $this->generateUrl('lc_dashboard'),
81
        ];
82
    }
83
84
    /**
85
     * @Route("/task/verify-phone/{id}/resend", name="lc_phone_verification_code_resend")
86
     * @Template()
87
     */
88
    public function resendAction(Request $request, $id)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
89
    {
90
        /** @var PhoneVerificationServiceInterface $phoneVerificationService */
91
        $phoneVerificationService = $this->get('phone_verification');
92
93
        $verification = $phoneVerificationService->getPendingPhoneVerificationById($id, $this->getUser());
94
        if (!$verification instanceof PhoneVerificationInterface) {
95
            throw $this->createNotFoundException();
96
        }
97
98
        try {
99
            $phoneVerificationService->sendVerificationCode($verification);
100
101
            $result = ['type' => 'success', 'message' => 'tasks.verify_phone.resend.success'];
102
        } catch (TooManyRequestsHttpException $e) {
103
            $result = ['type' => 'danger', 'message' => 'tasks.verify_phone.resend.errors.too_many_requests'];
104
        } catch (VerificationNotSentException $e) {
105
            $result = ['type' => 'danger', 'message' => 'tasks.verify_phone.resend.errors.unavailable'];
106
        }
107
108
        /** @var TranslatorInterface $translator */
109
        $translator = $this->get('translator');
110
        $this->addFlash($result['type'], $translator->trans($result['message']));
111
112
        return $this->redirectToRoute('lc_verify_phone', ['id' => $id]);
113
    }
114
115
    /**
116
     * @Route("/task/verify-phone/{id}/skip", name="lc_phone_verification_skip")
117
     * @Template()
118
     */
119
    public function skipAction(Request $request, $id)
0 ignored issues
show
Unused Code introduced by
The parameter $id is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
120
    {
121
        /** @var TaskStackManagerInterface $taskStackManager */
122
        $taskStackManager = $this->get('task_stack.manager');
123
124
        $task = $taskStackManager->getCurrentTask();
125
        if ($task) {
126
            $taskStackManager->setTaskSkipped($task);
127
        }
128
129
        return $taskStackManager->processRequest($request, $this->redirectToRoute('lc_dashboard'));
130
    }
131
132
    /**
133
     * This route is used to verify the phone using a Verification Token.
134
     *
135
     * The objective is that the user can simply click a link received via SMS and the phone would be verified
136
     *
137
     * @Route("/v/{id}/{token}", name="lc_phone_verification_verify_link")
138
     * @Template()
139
     */
140
    public function clickToVerifyAction(Request $request, $id, $token)
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
141
    {
142
        /** @var PhoneVerificationServiceInterface $phoneVerificationService */
143
        $phoneVerificationService = $this->get('phone_verification');
144
145
        $verification = $phoneVerificationService->getPhoneVerificationById($id);
146
147
        if (!$verification || false === $phoneVerificationService->verifyToken($verification, $token)) {
148
            /** @var TranslatorInterface $translator */
149
            $translator = $this->get('translator');
150
151
            return $this->render(
152
                'LoginCidadaoPhoneVerificationBundle:Verification:response.html.twig',
153
                [
154
                    'message' => $translator->trans('tasks.verify_phone.failure'),
155
                    'target' => $this->generateUrl('lc_dashboard'),
156
                ]
157
            );
158
        }
159
160
        return $this->redirectToRoute('lc_phone_verification_success', ['id' => $verification->getId()]);
161
    }
162
163
    private function noVerificationOrVerified(Request $request)
164
    {
165
        /** @var TaskStackManagerInterface $taskStackManager */
166
        $taskStackManager = $this->get('task_stack.manager');
167
        $task = $taskStackManager->getCurrentTask();
168
        if ($task) {
169
            $taskStackManager->setTaskSkipped($task);
170
        }
171
172
        return $taskStackManager->processRequest($request, $this->redirectToRoute('lc_dashboard'));
173
    }
174
175
    private function getNextResendDate(PhoneVerificationInterface $verification)
176
    {
177
        /** @var PhoneVerificationServiceInterface $phoneVerificationService */
178
        $phoneVerificationService = $this->get('phone_verification');
179
180
        $nextResend = $phoneVerificationService->getNextResendDate($verification);
181
        if ($nextResend <= new \DateTime()) {
182
            $nextResend = false;
183
        }
184
185
        return $nextResend;
186
    }
187
}
188