aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/UserBundle
diff options
context:
space:
mode:
authorJeremy Benoist <jeremy.benoist@gmail.com>2016-09-30 21:01:36 +0200
committerJeremy Benoist <jeremy.benoist@gmail.com>2016-09-30 21:01:36 +0200
commitca17abce2d3963e266bee905ab084ddfa7e1ff18 (patch)
treece33b43a21acc99300d7046387f99d437f713785 /src/Wallabag/UserBundle
parent114c55c0a6eade2ba6c53fe25f61cc58cca91620 (diff)
downloadwallabag-ca17abce2d3963e266bee905ab084ddfa7e1ff18.tar.gz
wallabag-ca17abce2d3963e266bee905ab084ddfa7e1ff18.tar.zst
wallabag-ca17abce2d3963e266bee905ab084ddfa7e1ff18.zip
Create user config in one place
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
Diffstat (limited to 'src/Wallabag/UserBundle')
-rw-r--r--src/Wallabag/UserBundle/EventListener/CreateConfigListener.php62
-rw-r--r--src/Wallabag/UserBundle/Resources/config/services.yml12
2 files changed, 74 insertions, 0 deletions
diff --git a/src/Wallabag/UserBundle/EventListener/CreateConfigListener.php b/src/Wallabag/UserBundle/EventListener/CreateConfigListener.php
new file mode 100644
index 00000000..15f4ac3d
--- /dev/null
+++ b/src/Wallabag/UserBundle/EventListener/CreateConfigListener.php
@@ -0,0 +1,62 @@
1<?php
2
3namespace Wallabag\UserBundle\EventListener;
4
5use Doctrine\ORM\EntityManager;
6use FOS\UserBundle\Event\UserEvent;
7use FOS\UserBundle\FOSUserEvents;
8use Symfony\Component\EventDispatcher\EventDispatcherInterface;
9use Symfony\Component\EventDispatcher\EventSubscriberInterface;
10use Wallabag\CoreBundle\Entity\Config;
11
12/**
13 * This listener will create the associated configuration when a user register.
14 * This configuration will be created right after the registration (no matter if it needs an email validation).
15 */
16class CreateConfigListener implements EventSubscriberInterface
17{
18 private $em;
19 private $theme;
20 private $itemsOnPage;
21 private $rssLimit;
22 private $language;
23 private $readingSpeed;
24
25 public function __construct(EntityManager $em, $theme, $itemsOnPage, $rssLimit, $language, $readingSpeed)
26 {
27 $this->em = $em;
28 $this->theme = $theme;
29 $this->itemsOnPage = $itemsOnPage;
30 $this->rssLimit = $rssLimit;
31 $this->language = $language;
32 $this->readingSpeed = $readingSpeed;
33 }
34
35 public static function getSubscribedEvents()
36 {
37 return [
38 // when a user register using the normal form
39 FOSUserEvents::REGISTRATION_COMPLETED => 'createConfig',
40 // when we manually create a user using the command line
41 // OR when we create it from the config UI
42 FOSUserEvents::USER_CREATED => 'createConfig',
43 ];
44 }
45
46 public function createConfig(UserEvent $event, $eventName = null, EventDispatcherInterface $eventDispatcher = null)
47 {
48 if (!$event->getUser()->isEnabled()) {
49 return;
50 }
51
52 $config = new Config($event->getUser());
53 $config->setTheme($this->theme);
54 $config->setItemsPerPage($this->itemsOnPage);
55 $config->setRssLimit($this->rssLimit);
56 $config->setLanguage($this->language);
57 $config->setReadingSpeed($this->readingSpeed);
58
59 $this->em->persist($config);
60 $this->em->flush();
61 }
62}
diff --git a/src/Wallabag/UserBundle/Resources/config/services.yml b/src/Wallabag/UserBundle/Resources/config/services.yml
index 05830555..eb9c8e67 100644
--- a/src/Wallabag/UserBundle/Resources/config/services.yml
+++ b/src/Wallabag/UserBundle/Resources/config/services.yml
@@ -20,3 +20,15 @@ services:
20 factory: [ "@doctrine.orm.default_entity_manager", getRepository ] 20 factory: [ "@doctrine.orm.default_entity_manager", getRepository ]
21 arguments: 21 arguments:
22 - WallabagUserBundle:User 22 - WallabagUserBundle:User
23
24 wallabag_user.create_config:
25 class: Wallabag\UserBundle\EventListener\CreateConfigListener
26 arguments:
27 - "@doctrine.orm.entity_manager"
28 - "%wallabag_core.theme%"
29 - "%wallabag_core.items_on_page%"
30 - "%wallabag_core.rss_limit%"
31 - "%wallabag_core.language%"
32 - "%wallabag_core.reading_speed%"
33 tags:
34 - { name: kernel.event_subscriber }