Passed
Push — master ( ed3a21...c78a04 )
by Guido
06:24
created

ForgotPassword::new()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 26
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 26
c 0
b 0
f 0
rs 9.7333
cc 4
nc 6
nop 0
1
<?php
2
namespace Gvera\Controllers;
3
4
use Gvera\Helpers\http\Response;
5
use Gvera\Models\User;
6
use ReflectionException;
7
8
/**
9
 * Class ForgotPassword
10
 * @package Gvera\Controllers
11
 */
12
class ForgotPassword extends GvController
13
{
14
    public function index()
15
    {
16
    }
17
18
    /**
19
     * @throws \Doctrine\ORM\OptimisticLockException
20
     * @throws ReflectionException
21
     */
22
    public function new()
23
    {
24
        if (!$this->httpRequest->isPost()) {
25
            $this->redirectToIndex();
26
        }
27
28
        $forgotPassService = $this->getForgotPasswordService();
1 ignored issue
show
Bug introduced by
The method getForgotPasswordService() does not exist on Gvera\Controllers\ForgotPassword. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

28
        /** @scrutinizer ignore-call */ 
29
        $forgotPassService = $this->getForgotPasswordService();
Loading history...
29
        $entityManager = $this->getEntityManager();
1 ignored issue
show
Bug introduced by
The method getEntityManager() does not exist on Gvera\Controllers\ForgotPassword. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

29
        /** @scrutinizer ignore-call */ 
30
        $entityManager = $this->getEntityManager();
Loading history...
30
31
        $email = $this->httpRequest->getParameter('email');
32
33
        $userRepository = $entityManager->getRepository(User::class);
34
        $user = $userRepository->findOneBy(['email' => $email]);
35
36
        if (null === $user) {
37
            $this->httpResponse->response(new Response('something went wrong'));
38
            return;
39
        }
40
41
        if (!$forgotPassService->validateNewForgotPassword($user)) {
42
            $this->redirectToIndex();
43
            return;
44
        }
45
46
        $newKey = $forgotPassService->generateNewForgotPassword($user);
47
        $this->httpResponse->response(new Response('Key generated successfully: ' . $newKey));
48
    }
49
50
    /**
51
     * @throws ReflectionException
52
     */
53
    public function use()
54
    {
55
        $forgotPasswordService = $this->diContainer->get("forgotPasswordService");
56
        try {
57
            $forgotPasswordService->useForgotPassword($this->httpRequest->getParameter('key'));
58
        } catch (\Exception $e) {
59
            $this->redirectToIndex();
60
        }
61
    }
62
63
    /**
64
     * @throws ReflectionException
65
     */
66
    public function regenerate()
67
    {
68
        $session = $this->getSession();
1 ignored issue
show
Bug introduced by
The method getSession() does not exist on Gvera\Controllers\ForgotPassword. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

68
        /** @scrutinizer ignore-call */ 
69
        $session = $this->getSession();
Loading history...
69
        if (!($session->get('forgot_password')) || !$this->httpRequest->isPost()) {
70
            $session->destroy();
71
            $this->redirectToIndex();
72
        }
73
74
        $forgotPasswordService = $this->diContainer->get("forgotPasswordService");
75
        $forgotPasswordService->regeneratePassword(
76
            $session->get('forgot_password'),
77
            $this->httpRequest->getParameter('new_password')
78
        );
79
    }
80
81
    private function redirectToIndex()
82
    {
83
        $this->httpResponse->redirect('/forgotpassword');
84
    }
85
}
86