]> git.immae.eu Git - github/wallabag/wallabag.git/blame - src/Wallabag/UserBundle/EventListener/CreateConfigListener.php
Merge remote-tracking branch 'origin/master' into 2.1
[github/wallabag/wallabag.git] / src / Wallabag / UserBundle / EventListener / CreateConfigListener.php
CommitLineData
fcb1fba5
NL
1<?php
2
ca17abce 3namespace Wallabag\UserBundle\EventListener;
fcb1fba5 4
772d8c4b 5use Doctrine\ORM\EntityManager;
ca17abce 6use FOS\UserBundle\Event\UserEvent;
772d8c4b 7use FOS\UserBundle\FOSUserEvents;
619cc453
JB
8use Symfony\Component\EventDispatcher\EventDispatcherInterface;
9use Symfony\Component\EventDispatcher\EventSubscriberInterface;
fcb1fba5
NL
10use Wallabag\CoreBundle\Entity\Config;
11
ca17abce
JB
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
fcb1fba5
NL
17{
18 private $em;
772d8c4b
JB
19 private $theme;
20 private $itemsOnPage;
21 private $rssLimit;
22 private $language;
ca17abce 23 private $readingSpeed;
fcb1fba5 24
ca17abce 25 public function __construct(EntityManager $em, $theme, $itemsOnPage, $rssLimit, $language, $readingSpeed)
fcb1fba5 26 {
fcb1fba5 27 $this->em = $em;
772d8c4b
JB
28 $this->theme = $theme;
29 $this->itemsOnPage = $itemsOnPage;
30 $this->rssLimit = $rssLimit;
31 $this->language = $language;
ca17abce 32 $this->readingSpeed = $readingSpeed;
fcb1fba5
NL
33 }
34
35 public static function getSubscribedEvents()
36 {
4094ea47 37 return [
ca17abce
JB
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',
4094ea47 43 ];
fcb1fba5
NL
44 }
45
ca17abce 46 public function createConfig(UserEvent $event, $eventName = null, EventDispatcherInterface $eventDispatcher = null)
fcb1fba5
NL
47 {
48 if (!$event->getUser()->isEnabled()) {
49 return;
50 }
51
52 $config = new Config($event->getUser());
772d8c4b
JB
53 $config->setTheme($this->theme);
54 $config->setItemsPerPage($this->itemsOnPage);
55 $config->setRssLimit($this->rssLimit);
56 $config->setLanguage($this->language);
ca17abce
JB
57 $config->setReadingSpeed($this->readingSpeed);
58
fcb1fba5
NL
59 $this->em->persist($config);
60 $this->em->flush();
61 }
62}