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

ChangePasswordController::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
nc 1
nop 9
dl 0
loc 20
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
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
    public function __construct(
74
        EngineInterface $templating,
75
        $changePasswordActionTemplate,
76
        UserRepositoryInterface $userRepository,
77
        RouterInterface $router,
78
        FormFactoryInterface $formFactory,
79
        EventDispatcherInterface $eventDispatcher,
80
        FlashMessages $flashMessages,
81
        $formType,
82
        array $formValidationGroups
83
    ) {
84
        $this->templating = $templating;
85
        $this->changePasswordActionTemplate = $changePasswordActionTemplate;
86
        $this->userRepository = $userRepository;
87
        $this->router = $router;
88
        $this->formFactory = $formFactory;
89
        $this->eventDispatcher = $eventDispatcher;
90
        $this->flashMessages = $flashMessages;
91
        $this->formType = $formType;
92
        $this->formValidationGroups = $formValidationGroups;
93
    }
94
95
    public function changePasswordAction(Request $request, $token)
96
    {
97
        $user = $this->userRepository->findUserByPasswordResetToken($token);
98
        if (!($user instanceof ResettablePasswordInterface)) {
99
            throw new NotFoundHttpException();
100
        }
101
102
        if (!$user->getPasswordResetToken()->isNonExpired()) {
103
            throw new NotFoundHttpException();
104
        }
105
106
        $form = $this->formFactory->create(
107
            $this->formType,
108
            $user,
109
            ['validation_groups' => $this->formValidationGroups]
110
        );
111
112
        if ($form->handleRequest($request)->isSubmitted() && $form->isValid()) {
113
            $user->removePasswordResetToken();
114
115
            $this->eventDispatcher->dispatch(
116
                AdminSecurityEvents::CHANGE_PASSWORD,
117
                new ChangePasswordEvent($user)
118
            );
119
120
            $this->flashMessages->success(
121
                'admin.password_reset.change_password.message.success',
122
                [],
123
                'FSiAdminSecurity'
124
            );
125
126
            return new RedirectResponse($this->router->generate('fsi_admin_security_user_login'));
127
        }
128
129
        return $this->templating->renderResponse(
130
            $this->changePasswordActionTemplate,
131
            ['form' => $form->createView()]
132
        );
133
    }
134
}
135