Passed
Pull Request — master (#103)
by Łukasz
02:43
created

ResetRequestController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 8
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

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
declare(strict_types=1);
11
12
namespace FSi\Bundle\AdminSecurityBundle\Controller\PasswordReset;
13
14
use FSi\Bundle\AdminBundle\Message\FlashMessages;
15
use FSi\Bundle\AdminSecurityBundle\Event\AdminSecurityEvents;
16
use FSi\Bundle\AdminSecurityBundle\Event\ResetPasswordRequestEvent;
17
use FSi\Bundle\AdminSecurityBundle\Security\User\ResettablePasswordInterface;
18
use FSi\Bundle\AdminSecurityBundle\Security\User\UserRepositoryInterface;
19
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
20
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
21
use Symfony\Component\Form\FormFactoryInterface;
22
use Symfony\Component\Form\FormInterface;
23
use Symfony\Component\HttpFoundation\RedirectResponse;
24
use Symfony\Component\HttpFoundation\Request;
25
use Symfony\Component\HttpFoundation\Response;
26
use Symfony\Component\Routing\RouterInterface;
27
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
28
use Symfony\Component\Security\Core\User\UserInterface;
29
30
class ResetRequestController
31
{
32
    /**
33
     * @var EngineInterface
34
     */
35
    private $templating;
36
37
    /**
38
     * @var string
39
     */
40
    private $requestActionTemplate;
41
42
    /**
43
     * @var FormFactoryInterface
44
     */
45
    private $formFactory;
46
47
    /**
48
     * @var RouterInterface
49
     */
50
    private $router;
51
52
    /**
53
     * @var UserRepositoryInterface
54
     */
55
    private $userRepository;
56
57
    /**
58
     * @var EventDispatcherInterface
59
     */
60
    private $eventDispatcher;
61
62
    /**
63
     * @var FlashMessages
64
     */
65
    private $flashMessages;
66
67
    /**
68
     * @var string
69
     */
70
    private $formType;
71
72
    public function __construct(
73
        EngineInterface $templating,
74
        $requestActionTemplate,
75
        FormFactoryInterface $formFactory,
76
        RouterInterface $router,
77
        UserRepositoryInterface $userRepository,
78
        EventDispatcherInterface $eventDispatcher,
79
        FlashMessages $flashMessages,
80
        $formType
81
    ) {
82
        $this->templating = $templating;
83
        $this->requestActionTemplate = $requestActionTemplate;
84
        $this->formFactory = $formFactory;
85
        $this->router = $router;
86
        $this->userRepository = $userRepository;
87
        $this->eventDispatcher = $eventDispatcher;
88
        $this->flashMessages = $flashMessages;
89
        $this->formType = $formType;
90
    }
91
92
    public function requestAction(Request $request): Response
93
    {
94
        $form = $this->formFactory->create($this->formType);
95
96
        if ($form->handleRequest($request)->isSubmitted() && $form->isValid()) {
97
            $user = $this->getUser($form);
98
            $redirectResponse = $this->addFlashAndRedirect(
99
                'info',
100
                'admin.password_reset.request.mail_sent_if_correct'
101
            );
102
103
            if (!($user instanceof ResettablePasswordInterface)) {
104
                return $redirectResponse;
105
            }
106
107
            if (($user instanceof AdvancedUserInterface) && !$user->isEnabled()) {
108
                return $redirectResponse;
109
            }
110
111
            if ($this->hasNonExpiredPasswordResetToken($user)) {
112
                return $redirectResponse;
113
            }
114
115
            if (($user instanceof AdvancedUserInterface) && !$user->isAccountNonLocked()) {
116
                return $redirectResponse;
117
            }
118
119
            $this->eventDispatcher->dispatch(
120
                AdminSecurityEvents::RESET_PASSWORD_REQUEST,
121
                new ResetPasswordRequestEvent($user)
122
            );
123
124
            return $redirectResponse;
125
        }
126
127
        return $this->templating->renderResponse(
128
            $this->requestActionTemplate,
129
            ['form' => $form->createView()]
130
        );
131
    }
132
133
    private function addFlashAndRedirect(string $type, string $message): RedirectResponse
134
    {
135
        $this->flashMessages->{$type}($message, [], 'FSiAdminSecurity');
136
137
        return new RedirectResponse($this->router->generate('fsi_admin_security_user_login'));
138
    }
139
140
    private function getUser(FormInterface $form): UserInterface
141
    {
142
        return $this->userRepository->findUserByEmail($form->get('email')->getData());
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->userReposi...et('email')->getData()) could return the type null which is incompatible with the type-hinted return Symfony\Component\Security\Core\User\UserInterface. Consider adding an additional type-check to rule them out.
Loading history...
143
    }
144
145
    private function hasNonExpiredPasswordResetToken(ResettablePasswordInterface $user): bool
146
    {
147
        return $user->getPasswordResetToken() && $user->getPasswordResetToken()->isNonExpired();
148
    }
149
}
150