namespace Wallabag\CoreBundle\Command;
+use FOS\UserBundle\Event\UserEvent;
+use FOS\UserBundle\FOSUserEvents;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\ArrayInput;
$em->persist($user);
- $config = new Config($user);
- $config->setTheme($this->getContainer()->getParameter('wallabag_core.theme'));
- $config->setItemsPerPage($this->getContainer()->getParameter('wallabag_core.items_on_page'));
- $config->setRssLimit($this->getContainer()->getParameter('wallabag_core.rss_limit'));
- $config->setReadingSpeed($this->getContainer()->getParameter('wallabag_core.reading_speed'));
- $config->setLanguage($this->getContainer()->getParameter('wallabag_core.language'));
-
- $em->persist($config);
+ // dispatch a created event so the associated config will be created
+ $event = new UserEvent($user);
+ $this->getContainer()->get('event_dispatcher')->dispatch(FOSUserEvents::USER_CREATED, $event);
$this->defaultOutput->writeln('');
namespace Wallabag\CoreBundle\Controller;
+use FOS\UserBundle\Event\UserEvent;
+use FOS\UserBundle\FOSUserEvents;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\JsonResponse;
$newUserForm->handleRequest($request);
if ($newUserForm->isValid() && $this->get('security.authorization_checker')->isGranted('ROLE_SUPER_ADMIN')) {
- $userManager->updateUser($newUser, true);
+ $userManager->updateUser($newUser);
- $config = new Config($newUser);
- $config->setTheme($this->getParameter('wallabag_core.theme'));
- $config->setItemsPerPage($this->getParameter('wallabag_core.items_on_page'));
- $config->setRssLimit($this->getParameter('wallabag_core.rss_limit'));
- $config->setLanguage($this->getParameter('wallabag_core.language'));
- $config->setReadingSpeed($this->getParameter('wallabag_core.reading_speed'));
-
- $em->persist($config);
-
- $em->flush();
+ // dispatch a created event so the associated config will be created
+ $event = new UserEvent($newUser, $request);
+ $this->get('event_dispatcher')->dispatch(FOSUserEvents::USER_CREATED, $event);
$this->get('session')->getFlashBag()->add(
'notice',
->getRepository('WallabagCoreBundle:Config')
->findOneByUser($this->getUser());
+ // should NEVER HAPPEN ...
if (!$config) {
$config = new Config($this->getUser());
}
arguments:
- WallabagCoreBundle:Tag
- wallabag_core.registration_confirmed:
- class: Wallabag\CoreBundle\EventListener\RegistrationConfirmedListener
- arguments:
- - "@doctrine.orm.entity_manager"
- - "%wallabag_core.theme%"
- - "%wallabag_core.items_on_page%"
- - "%wallabag_core.rss_limit%"
- - "%wallabag_core.language%"
- tags:
- - { name: kernel.event_subscriber }
-
wallabag_core.helper.entries_export:
class: Wallabag\CoreBundle\Helper\EntriesExport
arguments:
<?php
-namespace Wallabag\CoreBundle\EventListener;
+namespace Wallabag\UserBundle\EventListener;
use Doctrine\ORM\EntityManager;
-use FOS\UserBundle\Event\FilterUserResponseEvent;
+use FOS\UserBundle\Event\UserEvent;
use FOS\UserBundle\FOSUserEvents;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Wallabag\CoreBundle\Entity\Config;
-class RegistrationConfirmedListener implements EventSubscriberInterface
+/**
+ * This listener will create the associated configuration when a user register.
+ * This configuration will be created right after the registration (no matter if it needs an email validation).
+ */
+class CreateConfigListener implements EventSubscriberInterface
{
private $em;
private $theme;
private $itemsOnPage;
private $rssLimit;
private $language;
+ private $readingSpeed;
- public function __construct(EntityManager $em, $theme, $itemsOnPage, $rssLimit, $language)
+ public function __construct(EntityManager $em, $theme, $itemsOnPage, $rssLimit, $language, $readingSpeed)
{
$this->em = $em;
$this->theme = $theme;
$this->itemsOnPage = $itemsOnPage;
$this->rssLimit = $rssLimit;
$this->language = $language;
+ $this->readingSpeed = $readingSpeed;
}
public static function getSubscribedEvents()
{
return [
- FOSUserEvents::REGISTRATION_CONFIRMED => 'authenticate',
+ // when a user register using the normal form
+ FOSUserEvents::REGISTRATION_COMPLETED => 'createConfig',
+ // when we manually create a user using the command line
+ // OR when we create it from the config UI
+ FOSUserEvents::USER_CREATED => 'createConfig',
];
}
- public function authenticate(FilterUserResponseEvent $event, $eventName = null, EventDispatcherInterface $eventDispatcher = null)
+ public function createConfig(UserEvent $event, $eventName = null, EventDispatcherInterface $eventDispatcher = null)
{
if (!$event->getUser()->isEnabled()) {
return;
$config->setItemsPerPage($this->itemsOnPage);
$config->setRssLimit($this->rssLimit);
$config->setLanguage($this->language);
+ $config->setReadingSpeed($this->readingSpeed);
+
$this->em->persist($config);
$this->em->flush();
}
factory: [ "@doctrine.orm.default_entity_manager", getRepository ]
arguments:
- WallabagUserBundle:User
+
+ wallabag_user.create_config:
+ class: Wallabag\UserBundle\EventListener\CreateConfigListener
+ arguments:
+ - "@doctrine.orm.entity_manager"
+ - "%wallabag_core.theme%"
+ - "%wallabag_core.items_on_page%"
+ - "%wallabag_core.rss_limit%"
+ - "%wallabag_core.language%"
+ - "%wallabag_core.reading_speed%"
+ tags:
+ - { name: kernel.event_subscriber }
<?php
-namespace Tests\Wallabag\CoreBundle\EventListener;
+namespace Tests\Wallabag\UserBundle\EventListener;
use FOS\UserBundle\Event\FilterUserResponseEvent;
use FOS\UserBundle\FOSUserEvents;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Wallabag\CoreBundle\Entity\Config;
-use Wallabag\CoreBundle\EventListener\RegistrationConfirmedListener;
+use Wallabag\UserBundle\EventListener\CreateConfigListener;
use Wallabag\UserBundle\Entity\User;
-class RegistrationConfirmedListenerTest extends \PHPUnit_Framework_TestCase
+class CreateConfigListenerTest extends \PHPUnit_Framework_TestCase
{
private $em;
private $listener;
->disableOriginalConstructor()
->getMock();
- $this->listener = new RegistrationConfirmedListener(
+ $this->listener = new CreateConfigListener(
$this->em,
'baggy',
20,
50,
- 'fr'
+ 'fr',
+ 1
);
$this->dispatcher = new EventDispatcher();
$this->em->expects($this->never())->method('flush');
$this->dispatcher->dispatch(
- FOSUserEvents::REGISTRATION_CONFIRMED,
+ FOSUserEvents::REGISTRATION_COMPLETED,
$event
);
}
$config->setItemsPerPage(20);
$config->setRssLimit(50);
$config->setLanguage('fr');
+ $config->setReadingSpeed(1);
$this->em->expects($this->once())
->method('persist')
->method('flush');
$this->dispatcher->dispatch(
- FOSUserEvents::REGISTRATION_CONFIRMED,
+ FOSUserEvents::REGISTRATION_COMPLETED,
$event
);
}