Completed
Pull Request — master (#91)
by Piotr
02:36
created

ChangePasswordController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
c 0
b 0
f 0
rs 9.3142
cc 1
eloc 19
nc 1
nop 9

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
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\ChangePasswordEvent;
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\HttpFoundation\RedirectResponse;
21
use Symfony\Component\HttpFoundation\Request;
22
use Symfony\Component\HttpFoundation\Response;
23
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
24
use Symfony\Component\Routing\RouterInterface;
25
26
class ChangePasswordController
27
{
28
    /**
29
     * @var EngineInterface
30
     */
31
    private $templating;
32
33
    /**
34
     * @var string
35
     */
36
    private $changePasswordActionTemplate;
37
38
    /**
39
     * @var UserRepositoryInterface
40
     */
41
    private $userRepository;
42
43
    /**
44
     * @var RouterInterface
45
     */
46
    private $router;
47
48
    /**
49
     * @var FormFactoryInterface
50
     */
51
    private $formFactory;
52
53
    /**
54
     * @var EventDispatcherInterface
55
     */
56
    private $eventDispatcher;
57
58
    /**
59
     * @var FlashMessages
60
     */
61
    private $flashMessages;
62
63
    /**
64
     * @var string
65
     */
66
    private $formType;
67
68
    /**
69
     * @var array
70
     */
71
    private $formValidationGroups;
72
73
    /**
74
     * @param EngineInterface $templating
75
     * @param string $changePasswordActionTemplate
76
     * @param UserRepositoryInterface $userRepository
77
     * @param RouterInterface $router
78
     * @param FormFactoryInterface $formFactory
79
     * @param EventDispatcherInterface $eventDispatcher
80
     * @param FlashMessages $flashMessages
81
     * @param string $formType
82
     * @param array $formValidationGroups
83
     */
84
    public function __construct(
85
        EngineInterface $templating,
86
        $changePasswordActionTemplate,
87
        UserRepositoryInterface $userRepository,
88
        RouterInterface $router,
89
        FormFactoryInterface $formFactory,
90
        EventDispatcherInterface $eventDispatcher,
91
        FlashMessages $flashMessages,
92
        $formType,
93
        array $formValidationGroups
94
    ) {
95
        $this->templating = $templating;
96
        $this->changePasswordActionTemplate = $changePasswordActionTemplate;
97
        $this->userRepository = $userRepository;
98
        $this->router = $router;
99
        $this->formFactory = $formFactory;
100
        $this->eventDispatcher = $eventDispatcher;
101
        $this->flashMessages = $flashMessages;
102
        $this->formType = $formType;
103
        $this->formValidationGroups = $formValidationGroups;
104
    }
105
106
    /**
107
     * @param Request $request
108
     * @param string $token
109
     * @return RedirectResponse|Response
110
     */
111
    public function changePasswordAction(Request $request, $token)
112
    {
113
        $user = $this->userRepository->findUserByPasswordResetToken($token);
114
        if (!($user instanceof ResettablePasswordInterface)) {
115
            throw new NotFoundHttpException();
116
        }
117
118
        if (!$user->getPasswordResetToken()->isNonExpired()) {
119
            throw new NotFoundHttpException();
120
        }
121
122
        $form = $this->formFactory->create(
123
            $this->formType,
124
            $user,
125
            ['validation_groups' => $this->formValidationGroups]
126
        );
127
128
        if ($form->handleRequest($request)->isSubmitted() && $form->isValid()) {
129
            $user->removePasswordResetToken();
130
131
            $this->eventDispatcher->dispatch(
132
                AdminSecurityEvents::CHANGE_PASSWORD,
133
                new ChangePasswordEvent($user)
134
            );
135
136
            $this->flashMessages->success('admin.password_reset.change_password.message.success', 'FSiAdminSecurity');
137
138
            return new RedirectResponse($this->router->generate('fsi_admin_security_user_login'));
139
        }
140
141
        return $this->templating->renderResponse(
142
            $this->changePasswordActionTemplate,
143
            ['form' => $form->createView()]
144
        );
145
    }
146
}
147