Skip to content

Commit 71d0abf

Browse files
committed
Refactored the GroupController to use events
1 parent 09363a2 commit 71d0abf

File tree

12 files changed

+181
-118
lines changed

12 files changed

+181
-118
lines changed

Controller/GroupController.php

Lines changed: 75 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@
1111

1212
namespace FOS\UserBundle\Controller;
1313

14+
use FOS\UserBundle\FOSUserEvents;
15+
use FOS\UserBundle\Event\FormEvent;
16+
use FOS\UserBundle\Event\GroupResponseEvent;
1417
use Symfony\Component\DependencyInjection\ContainerAware;
18+
use Symfony\Component\HttpFoundation\Request;
1519
use Symfony\Component\HttpFoundation\RedirectResponse;
1620
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
1721

@@ -36,51 +40,91 @@ public function listAction()
3640
/**
3741
* Show one group
3842
*/
39-
public function showAction($groupname)
43+
public function showAction($groupName)
4044
{
41-
$group = $this->findGroupBy('name', $groupname);
45+
$group = $this->findGroupBy('name', $groupName);
4246

4347
return $this->container->get('templating')->renderResponse('FOSUserBundle:Group:show.html.'.$this->getEngine(), array('group' => $group));
4448
}
4549

4650
/**
4751
* Edit one group, show the edit form
4852
*/
49-
public function editAction($groupname)
53+
public function editAction(Request $request, $groupName)
5054
{
51-
$group = $this->findGroupBy('name', $groupname);
52-
$form = $this->container->get('fos_user.group.form');
53-
$formHandler = $this->container->get('fos_user.group.form.handler');
55+
$group = $this->findGroupBy('name', $groupName);
56+
/** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
57+
$formFactory = $this->container->get('fos_user.group.form.factory');
5458

55-
$process = $formHandler->process($group);
56-
if ($process) {
57-
$this->setFlash('fos_user_success', 'group.flash.updated');
58-
$groupUrl = $this->container->get('router')->generate('fos_user_group_show', array('groupname' => $group->getName()));
59+
$form = $formFactory->createForm();
60+
$form->setData($group);
5961

60-
return new RedirectResponse($groupUrl);
62+
if ($request->isMethod('POST')) {
63+
$form->bind($request);
64+
65+
if ($form->isValid()) {
66+
/** @var $groupManager \FOS\UserBundle\Model\GroupManagerInterface */
67+
$groupManager = $this->container->get('fos_user.group_manager');
68+
/** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
69+
$dispatcher = $this->container->get('event_dispatcher');
70+
71+
$event = new FormEvent($form);
72+
$dispatcher->dispatch(FOSUserEvents::GROUP_EDIT_SUCCESS, $event);
73+
74+
$groupManager->updateGroup($group);
75+
76+
if (null === $response = $event->getResponse()) {
77+
$url = $this->container->get('router')->generate('fos_user_group_show', array('groupName' => $group->getName()));
78+
$response = new RedirectResponse($url);
79+
}
80+
81+
$dispatcher->dispatch(FOSUserEvents::GROUP_EDIT_COMPLETED, new GroupResponseEvent($group, $response));
82+
83+
return $response;
84+
}
6185
}
6286

6387
return $this->container->get('templating')->renderResponse('FOSUserBundle:Group:edit.html.'.$this->getEngine(), array(
6488
'form' => $form->createview(),
65-
'groupname' => $group->getName(),
89+
'group_name' => $group->getName(),
6690
));
6791
}
6892

6993
/**
7094
* Show the new form
7195
*/
72-
public function newAction()
96+
public function newAction(Request $request)
7397
{
74-
$form = $this->container->get('fos_user.group.form');
75-
$formHandler = $this->container->get('fos_user.group.form.handler');
98+
/** @var $groupManager \FOS\UserBundle\Model\GroupManagerInterface */
99+
$groupManager = $this->container->get('fos_user.group_manager');
100+
/** @var $formFactory \FOS\UserBundle\Form\Factory\FactoryInterface */
101+
$formFactory = $this->container->get('fos_user.group.form.factory');
102+
103+
$group = $groupManager->createGroup('');
104+
$form = $formFactory->createForm();
105+
$form->setData($group);
106+
107+
if ($request->isMethod('POST')) {
108+
$form->bind($request);
109+
110+
if ($form->isValid()) {
111+
/** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
112+
$dispatcher = $this->container->get('event_dispatcher');
113+
114+
$event = new FormEvent($form);
115+
$dispatcher->dispatch(FOSUserEvents::GROUP_CREATE_SUCCESS, $event);
76116

77-
$process = $formHandler->process();
78-
if ($process) {
79-
$this->setFlash('fos_user_success', 'group.flash.created');
80-
$parameters = array('groupname' => $form->getData('group')->getName());
81-
$url = $this->container->get('router')->generate('fos_user_group_show', $parameters);
117+
$groupManager->updateGroup($group);
82118

83-
return new RedirectResponse($url);
119+
if (null === $response = $event->getResponse()) {
120+
$url = $this->container->get('router')->generate('fos_user_group_show', array('groupName' => $group->getName()));
121+
$response = new RedirectResponse($url);
122+
}
123+
124+
$dispatcher->dispatch(FOSUserEvents::GROUP_CREATE_COMPLETED, new GroupResponseEvent($group, $response));
125+
126+
return $response;
127+
}
84128
}
85129

86130
return $this->container->get('templating')->renderResponse('FOSUserBundle:Group:new.html.'.$this->getEngine(), array(
@@ -91,13 +135,18 @@ public function newAction()
91135
/**
92136
* Delete one group
93137
*/
94-
public function deleteAction($groupname)
138+
public function deleteAction($groupName)
95139
{
96-
$group = $this->findGroupBy('name', $groupname);
140+
$group = $this->findGroupBy('name', $groupName);
97141
$this->container->get('fos_user.group_manager')->deleteGroup($group);
98-
$this->setFlash('fos_user_success', 'group.flash.deleted');
99142

100-
return new RedirectResponse($this->container->get('router')->generate('fos_user_group_list'));
143+
$response = new RedirectResponse($this->container->get('router')->generate('fos_user_group_list'));
144+
145+
/** @var $dispatcher \Symfony\Component\EventDispatcher\EventDispatcherInterface */
146+
$dispatcher = $this->container->get('event_dispatcher');
147+
$dispatcher->dispatch(FOSUserEvents::GROUP_CREATE_COMPLETED, new GroupResponseEvent($group, $response));
148+
149+
return $response;
101150
}
102151

103152
/**
@@ -106,7 +155,7 @@ public function deleteAction($groupname)
106155
* @param string $key property name
107156
* @param mixed $value property value
108157
*
109-
* @throws NotFoundException if user does not exist
158+
* @throws NotFoundHttpException if user does not exist
110159
* @return \FOS\UserBundle\Model\GroupInterface
111160
*/
112161
protected function findGroupBy($key, $value)
@@ -126,13 +175,4 @@ protected function getEngine()
126175
{
127176
return $this->container->getParameter('fos_user.template.engine');
128177
}
129-
130-
/**
131-
* @param string $action
132-
* @param string $value
133-
*/
134-
protected function setFlash($action, $value)
135-
{
136-
$this->container->get('session')->setFlash($action, $value);
137-
}
138178
}

DependencyInjection/Configuration.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,6 @@ private function addGroupSection(ArrayNodeDefinition $node)
251251
->addDefaultsIfNotSet()
252252
->children()
253253
->scalarNode('type')->defaultValue('fos_user_group')->end()
254-
->scalarNode('handler')->defaultValue('fos_user.group.form.handler.default')->end()
255254
->scalarNode('name')->defaultValue('fos_user_group_form')->end()
256255
->arrayNode('validation_groups')
257256
->prototype('scalar')->end()

DependencyInjection/FOSUserExtension.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,6 @@ private function loadGroups(array $config, ContainerBuilder $container, XmlFileL
165165
}
166166

167167
$container->setAlias('fos_user.group_manager', $config['group_manager']);
168-
$container->setAlias('fos_user.group.form.handler', $config['form']['handler']);
169-
unset($config['form']['handler']);
170168

171169
$this->remapParametersNamespaces($config, $container, array(
172170
'' => array(

Event/GroupResponseEvent.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the FOSUserBundle package.
5+
*
6+
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace FOS\UserBundle\Event;
13+
14+
use FOS\UserBundle\Model\GroupInterface;
15+
use Symfony\Component\EventDispatcher\Event;
16+
use Symfony\Component\HttpFoundation\Response;
17+
18+
class GroupResponseEvent extends Event
19+
{
20+
private $group;
21+
private $response;
22+
23+
public function __construct(GroupInterface $group)
24+
{
25+
$this->group = $group;
26+
}
27+
28+
/**
29+
* @return GroupInterface
30+
*/
31+
public function getGroup()
32+
{
33+
return $this->group;
34+
}
35+
36+
public function setResponse(Response $response)
37+
{
38+
$this->response = $response;
39+
}
40+
41+
/**
42+
* @return Response|null
43+
*/
44+
public function getResponse()
45+
{
46+
return $this->response;
47+
}
48+
}

EventListener/FlashListener.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@ class FlashListener implements EventSubscriberInterface
2121
{
2222
private static $successMessages = array(
2323
FOSUserEvents::CHANGE_PASSWORD_SUCCESS => 'change_password.flash.updated',
24+
FOSUserEvents::GROUP_CREATE_SUCCESS => 'group.flash.created',
25+
FOSUserEvents::GROUP_DELETE_COMPLETED => 'group.flash.deleted',
26+
FOSUserEvents::GROUP_EDIT_SUCCESS => 'group.flash.updated',
2427
FOSUserEvents::PROFILE_EDIT_SUCCESS => 'profile.flash.updated',
2528
FOSUserEvents::REGISTRATION_SUCCESS => 'registration.flash.user_created',
26-
FOSUserEvents::RESETTING_RESET_SUCCESS => 'resetting.flash.success'
29+
FOSUserEvents::RESETTING_RESET_SUCCESS => 'resetting.flash.success',
2730
);
2831

2932
/**
@@ -42,9 +45,12 @@ public static function getSubscribedEvents()
4245
{
4346
return array(
4447
FOSUserEvents::CHANGE_PASSWORD_SUCCESS => 'addSuccessFlash',
48+
FOSUserEvents::GROUP_CREATE_SUCCESS => 'addSuccessFlash',
49+
FOSUserEvents::GROUP_DELETE_COMPLETED => 'addSuccessFlash',
50+
FOSUserEvents::GROUP_EDIT_SUCCESS => 'addSuccessFlash',
4551
FOSUserEvents::PROFILE_EDIT_SUCCESS => 'addSuccessFlash',
4652
FOSUserEvents::REGISTRATION_SUCCESS => 'addSuccessFlash',
47-
FOSUserEvents::RESETTING_RESET_SUCCESS => 'addSuccessFlash'
53+
FOSUserEvents::RESETTING_RESET_SUCCESS => 'addSuccessFlash',
4854
);
4955
}
5056

FOSUserEvents.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,46 @@ final class FOSUserEvents
3232
*/
3333
const CHANGE_PASSWORD_COMPLETED = 'fos_user.change_password.edit.completed';
3434

35+
/**
36+
* The GROUP_CREATE_SUCCESS event occurs when the group creation form is submitted successfully.
37+
*
38+
* This event allows you to set the response instead of using the default one.
39+
* The event listener method receives a FOS\UserBundle\Event\FormEvent instance.
40+
*/
41+
const GROUP_CREATE_SUCCESS = 'fos_user.group.create.success';
42+
43+
/**
44+
* The GROUP_CREATE_COMPLETED event occurs after saving the group in the group creation process.
45+
*
46+
* This event allows you to access the response which will be sent.
47+
* The event listener method receives a FOS\UserBundle\Event\GroupResponseEvent instance.
48+
*/
49+
const GROUP_CREATE_COMPLETED = 'fos_user.group.create.completed';
50+
51+
/**
52+
* The GROUP_DELETE_COMPLETED event occurs after deleting the group.
53+
*
54+
* This event allows you to access the response which will be sent.
55+
* The event listener method receives a FOS\UserBundle\Event\GroupResponseEvent instance.
56+
*/
57+
const GROUP_DELETE_COMPLETED = 'fos_user.group.delete.completed';
58+
59+
/**
60+
* The GROUP_EDIT_SUCCESS event occurs when the group edit form is submitted successfully.
61+
*
62+
* This event allows you to set the response instead of using the default one.
63+
* The event listener method receives a FOS\UserBundle\Event\FormEvent instance.
64+
*/
65+
const GROUP_EDIT_SUCCESS = 'fos_user.group.edit.success';
66+
67+
/**
68+
* The GROUP_EDIT_COMPLETED event occurs after saving the group in the group edit process.
69+
*
70+
* This event allows you to access the response which will be sent.
71+
* The event listener method receives a FOS\UserBundle\Event\GroupResponseEvent instance.
72+
*/
73+
const GROUP_EDIT_COMPLETED = 'fos_user.group.edit.completed';
74+
3575
/**
3676
* The PROFILE_EDIT_SUCCESS event occurs when the profile edit form is submitted successfully.
3777
*

Form/Handler/GroupFormHandler.php

Lines changed: 0 additions & 57 deletions
This file was deleted.

0 commit comments

Comments
 (0)