]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Create user config in one place
authorJeremy Benoist <jeremy.benoist@gmail.com>
Fri, 30 Sep 2016 19:01:36 +0000 (21:01 +0200)
committerJeremy Benoist <jeremy.benoist@gmail.com>
Fri, 30 Sep 2016 19:01:36 +0000 (21:01 +0200)
Using a listener, user config is now created when a user:

- is created from the command line
- register (with or without email confirmation)
- is created from the config panel

src/Wallabag/CoreBundle/Command/InstallCommand.php
src/Wallabag/CoreBundle/Controller/ConfigController.php
src/Wallabag/CoreBundle/Resources/config/services.yml
src/Wallabag/UserBundle/EventListener/CreateConfigListener.php [moved from src/Wallabag/CoreBundle/EventListener/RegistrationConfirmedListener.php with 53% similarity]
src/Wallabag/UserBundle/Resources/config/services.yml
tests/Wallabag/UserBundle/EventListener/CreateConfigListenerTest.php [moved from tests/Wallabag/CoreBundle/EventListener/RegistrationConfirmedListenerTest.php with 83% similarity]

index 3873d2d366762b88241874bb27415696b0b1cca3..cc7c2c94cf268be86cd4de51d895404a7d433ec2 100644 (file)
@@ -2,6 +2,8 @@
 
 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;
@@ -236,14 +238,9 @@ class InstallCommand extends ContainerAwareCommand
 
         $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('');
 
index 4f75511bf62cf5c7ee23b5ceb5eaeeb16a89248f..75a9af0b5c1f6bb5259f317b84e92f99f01bfaba 100644 (file)
@@ -2,6 +2,8 @@
 
 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;
@@ -133,18 +135,11 @@ class ConfigController extends Controller
         $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',
@@ -238,6 +233,7 @@ class ConfigController extends Controller
             ->getRepository('WallabagCoreBundle:Config')
             ->findOneByUser($this->getUser());
 
+        // should NEVER HAPPEN ...
         if (!$config) {
             $config = new Config($this->getUser());
         }
index 23e6d3ca91d2ea4f578a8ad0aab7aa16ce0f2e8f..5a727ec530c57c8dcb3f6ddbae3e9c461006d830 100644 (file)
@@ -88,17 +88,6 @@ services:
         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:
similarity index 53%
rename from src/Wallabag/CoreBundle/EventListener/RegistrationConfirmedListener.php
rename to src/Wallabag/UserBundle/EventListener/CreateConfigListener.php
index 10586126fe35f43ef5ae45a1d1e635686259adb0..15f4ac3dee41faad596d6b56484d3cc1858b6e41 100644 (file)
@@ -1,39 +1,49 @@
 <?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;
@@ -44,6 +54,8 @@ class RegistrationConfirmedListener implements EventSubscriberInterface
         $config->setItemsPerPage($this->itemsOnPage);
         $config->setRssLimit($this->rssLimit);
         $config->setLanguage($this->language);
+        $config->setReadingSpeed($this->readingSpeed);
+
         $this->em->persist($config);
         $this->em->flush();
     }
index 05830555ee8cb9eee5818598930e3c14fe8ccba4..eb9c8e676e0cbd03b4f92eda2b502a1f5c88ad32 100644 (file)
@@ -20,3 +20,15 @@ services:
         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 }
similarity index 83%
rename from tests/Wallabag/CoreBundle/EventListener/RegistrationConfirmedListenerTest.php
rename to tests/Wallabag/UserBundle/EventListener/CreateConfigListenerTest.php
index e45722fa78565ecf04fc8071fa3229d8d78e76ce..0cebd3e43731a23dba09040ab0b984a9574e75f1 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 
-namespace Tests\Wallabag\CoreBundle\EventListener;
+namespace Tests\Wallabag\UserBundle\EventListener;
 
 use FOS\UserBundle\Event\FilterUserResponseEvent;
 use FOS\UserBundle\FOSUserEvents;
@@ -8,10 +8,10 @@ use Symfony\Component\EventDispatcher\EventDispatcher;
 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;
@@ -25,12 +25,13 @@ class RegistrationConfirmedListenerTest extends \PHPUnit_Framework_TestCase
             ->disableOriginalConstructor()
             ->getMock();
 
-        $this->listener = new RegistrationConfirmedListener(
+        $this->listener = new CreateConfigListener(
             $this->em,
             'baggy',
             20,
             50,
-            'fr'
+            'fr',
+            1
         );
 
         $this->dispatcher = new EventDispatcher();
@@ -55,7 +56,7 @@ class RegistrationConfirmedListenerTest extends \PHPUnit_Framework_TestCase
         $this->em->expects($this->never())->method('flush');
 
         $this->dispatcher->dispatch(
-            FOSUserEvents::REGISTRATION_CONFIRMED,
+            FOSUserEvents::REGISTRATION_COMPLETED,
             $event
         );
     }
@@ -76,6 +77,7 @@ class RegistrationConfirmedListenerTest extends \PHPUnit_Framework_TestCase
         $config->setItemsPerPage(20);
         $config->setRssLimit(50);
         $config->setLanguage('fr');
+        $config->setReadingSpeed(1);
 
         $this->em->expects($this->once())
             ->method('persist')
@@ -84,7 +86,7 @@ class RegistrationConfirmedListenerTest extends \PHPUnit_Framework_TestCase
             ->method('flush');
 
         $this->dispatcher->dispatch(
-            FOSUserEvents::REGISTRATION_CONFIRMED,
+            FOSUserEvents::REGISTRATION_COMPLETED,
             $event
         );
     }