Completed
Push — master ( d67956...a656c9 )
by Piotr
11s
created

ResetRequestController::addFlashAndRedirect()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * (c) FSi sp. z o.o. <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace FSi\Bundle\AdminSecurityBundle\Controller\PasswordReset;
11
12
use FSi\Bundle\AdminBundle\Message\FlashMessages;
13
use FSi\Bundle\AdminSecurityBundle\Event\AdminSecurityEvents;
14
use FSi\Bundle\AdminSecurityBundle\Event\ResetPasswordRequestEvent;
15
use FSi\Bundle\AdminSecurityBundle\Security\User\ResettablePasswordInterface;
16
use FSi\Bundle\AdminSecurityBundle\Security\User\UserRepositoryInterface;
17
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
18
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
19
use Symfony\Component\Form\FormFactoryInterface;
20
use Symfony\Component\Form\FormInterface;
21
use Symfony\Component\HttpFoundation\RedirectResponse;
22
use Symfony\Component\HttpFoundation\Request;
23
use Symfony\Component\HttpFoundation\Response;
24
use Symfony\Component\Routing\RouterInterface;
25
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
26
27
class ResetRequestController
28
{
29
    /**
30
     * @var EngineInterface
31
     */
32
    private $templating;
33
34
    /**
35
     * @var string
36
     */
37
    private $requestActionTemplate;
38
39
    /**
40
     * @var FormFactoryInterface
41
     */
42
    private $formFactory;
43
44
    /**
45
     * @var RouterInterface
46
     */
47
    private $router;
48
49
    /**
50
     * @var UserRepositoryInterface
51
     */
52
    private $userRepository;
53
54
    /**
55
     * @var EventDispatcherInterface
56
     */
57
    private $eventDispatcher;
58
59
    /**
60
     * @var FlashMessages
61
     */
62
    private $flashMessages;
63
64
    /**
65
     * @var string
66
     */
67
    private $formType;
68
69
    public function __construct(
70
        EngineInterface $templating,
71
        $requestActionTemplate,
72
        FormFactoryInterface $formFactory,
73
        RouterInterface $router,
74
        UserRepositoryInterface $userRepository,
75
        EventDispatcherInterface $eventDispatcher,
76
        FlashMessages $flashMessages,
77
        $formType
78
    ) {
79
        $this->templating = $templating;
80
        $this->requestActionTemplate = $requestActionTemplate;
81
        $this->formFactory = $formFactory;
82
        $this->router = $router;
83
        $this->userRepository = $userRepository;
84
        $this->eventDispatcher = $eventDispatcher;
85
        $this->flashMessages = $flashMessages;
86
        $this->formType = $formType;
87
    }
88
89
    public function requestAction(Request $request)
90
    {
91
        $form = $this->formFactory->create($this->formType);
92
93
        if ($form->handleRequest($request)->isSubmitted() && $form->isValid()) {
94
            $user = $this->getUser($form);
95
            $redirectResponse = $this->addFlashAndRedirect(
96
                'info',
97
                'admin.password_reset.request.mail_sent_if_correct'
98
            );
99
100
            if (!($user instanceof ResettablePasswordInterface)) {
101
                return $redirectResponse;
102
            }
103
104
            if (!$user->isEnabled()) {
0 ignored issues
show
Bug introduced by
The method isEnabled() does not exist on FSi\Bundle\AdminSecurity...ttablePasswordInterface. Since it exists in all sub-types, consider adding an abstract or default implementation to FSi\Bundle\AdminSecurity...ttablePasswordInterface. ( Ignorable by Annotation )

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

104
            if (!$user->/** @scrutinizer ignore-call */ isEnabled()) {
Loading history...
105
                return $redirectResponse;
106
            }
107
108
            if ($this->hasNonExpiredPasswordResetToken($user)) {
109
                return $redirectResponse;
110
            }
111
112
            if (($user instanceof AdvancedUserInterface) && !$user->isAccountNonLocked()) {
113
                return $redirectResponse;
114
            }
115
116
            $this->eventDispatcher->dispatch(
117
                AdminSecurityEvents::RESET_PASSWORD_REQUEST,
118
                new ResetPasswordRequestEvent($user)
119
            );
120
121
            return $redirectResponse;
122
        }
123
124
        return $this->templating->renderResponse(
125
            $this->requestActionTemplate,
126
            ['form' => $form->createView()]
127
        );
128
    }
129
130
    /**
131
     * @param string $type
132
     * @param string $message
133
     * @return RedirectResponse
134
     */
135
    private function addFlashAndRedirect($type, $message)
136
    {
137
        $this->flashMessages->{$type}($message, [], 'FSiAdminSecurity');
138
139
        return new RedirectResponse($this->router->generate('fsi_admin_security_user_login'));
140
    }
141
142
    /**
143
     * @param FormInterface $form
144
     * @return ResettablePasswordInterface|null
145
     */
146
    private function getUser(FormInterface $form)
147
    {
148
        return $this->userRepository->findUserByEmail($form->get('email')->getData());
149
    }
150
151
    /**
152
     * @param ResettablePasswordInterface $user
153
     * @return bool
154
     */
155
    private function hasNonExpiredPasswordResetToken(ResettablePasswordInterface $user)
156
    {
157
        return $user->getPasswordResetToken() && $user->getPasswordResetToken()->isNonExpired();
158
    }
159
}
160