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

AdminController::changePasswordAction()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 30
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 30
c 1
b 0
f 0
rs 8.5806
cc 4
eloc 17
nc 3
nop 1
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 \Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface
29
     */
30
    private $tokenStorage;
31
32
    /**
33
     * @var \Symfony\Component\Routing\RouterInterface
34
     */
35
    private $router;
36
37
    /**
38
     * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface
39
     */
40
    private $eventDispatcher;
41
42
    /**
43
     * @var \Symfony\Bundle\FrameworkBundle\Templating\EngineInterface
44
     */
45
    private $templating;
46
47
    /**
48
     * @var \Symfony\Component\Form\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
    /**
73
     * @param EngineInterface $templating
74
     * @param FormFactoryInterface $formFactory
75
     * @param TokenStorageInterface $tokenStorage
76
     * @param RouterInterface $router
77
     * @param EventDispatcherInterface $eventDispatcher
78
     * @param FlashMessages $flashMessages
79
     * @param string $changePasswordActionTemplate
80
     * @param string $changePasswordFormType
81
     * @param array $changePasswordFormValidationGroups
82
     */
83
    public function __construct(
84
        EngineInterface $templating,
85
        FormFactoryInterface $formFactory,
86
        TokenStorageInterface $tokenStorage,
87
        RouterInterface $router,
88
        EventDispatcherInterface $eventDispatcher,
89
        FlashMessages $flashMessages,
90
        $changePasswordActionTemplate,
91
        $changePasswordFormType,
92
        array $changePasswordFormValidationGroups
93
    ) {
94
        $this->templating = $templating;
95
        $this->formFactory = $formFactory;
96
        $this->tokenStorage = $tokenStorage;
97
        $this->router = $router;
98
        $this->eventDispatcher = $eventDispatcher;
99
        $this->flashMessages = $flashMessages;
100
        $this->changePasswordActionTemplate = $changePasswordActionTemplate;
101
        $this->changePasswordFormType = $changePasswordFormType;
102
        $this->changePasswordFormValidationGroups = $changePasswordFormValidationGroups;
103
    }
104
105
    public function changePasswordAction(Request $request)
106
    {
107
        $user = $this->tokenStorage->getToken()->getUser();
108
        if (!($user instanceof ChangeablePasswordInterface)) {
109
            throw new NotFoundHttpException();
110
        }
111
112
        $form = $this->formFactory->create(
113
            $this->changePasswordFormType,
114
            $user,
115
            ['validation_groups' => $this->changePasswordFormValidationGroups]
116
        );
117
118
        if ($form->handleRequest($request)->isSubmitted() && $form->isValid()) {
119
120
            $this->eventDispatcher->dispatch(
121
                AdminSecurityEvents::CHANGE_PASSWORD,
122
                new ChangePasswordEvent($user)
123
            );
124
125
            $this->flashMessages->success('admin.change_password_message.success', 'FSiAdminSecurity');
126
127
            return new RedirectResponse($this->router->generate('fsi_admin_security_user_login'));
128
        }
129
130
        return $this->templating->renderResponse(
131
            $this->changePasswordActionTemplate,
132
            ['form' => $form->createView()]
133
        );
134
    }
135
}
136