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

AdminController::__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;
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\ChangeablePasswordInterface;
16
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
17
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
18
use Symfony\Component\Form\FormFactoryInterface;
19
use Symfony\Component\HttpFoundation\RedirectResponse;
20
use Symfony\Component\HttpFoundation\Request;
21
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
22
use Symfony\Component\Routing\RouterInterface;
23
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
24
25
class AdminController
26
{
27
    /**
28
     * @var TokenStorageInterface
29
     */
30
    private $tokenStorage;
31
32
    /**
33
     * @var RouterInterface
34
     */
35
    private $router;
36
37
    /**
38
     * @var EventDispatcherInterface
39
     */
40
    private $eventDispatcher;
41
42
    /**
43
     * @var EngineInterface
44
     */
45
    private $templating;
46
47
    /**
48
     * @var FormFactoryInterface
49
     */
50
    private $formFactory;
51
52
    /**
53
     * @var string
54
     */
55
    private $changePasswordActionTemplate;
56
57
    /**
58
     * @var FlashMessages
59
     */
60
    private $flashMessages;
61
62
    /**
63
     * @var string
64
     */
65
    private $changePasswordFormType;
66
67
    /**
68
     * @var array
69
     */
70
    private $changePasswordFormValidationGroups;
71
72
    public function __construct(
73
        EngineInterface $templating,
74
        FormFactoryInterface $formFactory,
75
        TokenStorageInterface $tokenStorage,
76
        RouterInterface $router,
77
        EventDispatcherInterface $eventDispatcher,
78
        FlashMessages $flashMessages,
79
        string $changePasswordActionTemplate,
80
        string $changePasswordFormType,
81
        array $changePasswordFormValidationGroups
82
    ) {
83
        $this->templating = $templating;
84
        $this->formFactory = $formFactory;
85
        $this->tokenStorage = $tokenStorage;
86
        $this->router = $router;
87
        $this->eventDispatcher = $eventDispatcher;
88
        $this->flashMessages = $flashMessages;
89
        $this->changePasswordActionTemplate = $changePasswordActionTemplate;
90
        $this->changePasswordFormType = $changePasswordFormType;
91
        $this->changePasswordFormValidationGroups = $changePasswordFormValidationGroups;
92
    }
93
94
    public function changePasswordAction(Request $request)
95
    {
96
        $user = $this->tokenStorage->getToken()->getUser();
97
        if (!($user instanceof ChangeablePasswordInterface)) {
98
            throw new NotFoundHttpException();
99
        }
100
101
        $form = $this->formFactory->create(
102
            $this->changePasswordFormType,
103
            $user,
104
            ['validation_groups' => $this->changePasswordFormValidationGroups]
105
        );
106
107
        if ($form->handleRequest($request)->isSubmitted() && $form->isValid()) {
108
109
            $this->eventDispatcher->dispatch(
110
                AdminSecurityEvents::CHANGE_PASSWORD,
111
                new ChangePasswordEvent($user)
112
            );
113
114
            $this->flashMessages->success(
115
                'admin.change_password_message.success',
116
                [],
117
                'FSiAdminSecurity'
118
            );
119
120
            return new RedirectResponse($this->router->generate('fsi_admin_security_user_login'));
121
        }
122
123
        return $this->templating->renderResponse(
124
            $this->changePasswordActionTemplate,
125
            ['form' => $form->createView()]
126
        );
127
    }
128
}
129