]>
Commit | Line | Data |
---|---|---|
fcb1fba5 NL |
1 | <?php |
2 | ||
ca17abce | 3 | namespace Wallabag\UserBundle\EventListener; |
fcb1fba5 | 4 | |
772d8c4b | 5 | use Doctrine\ORM\EntityManager; |
ca17abce | 6 | use FOS\UserBundle\Event\UserEvent; |
772d8c4b | 7 | use FOS\UserBundle\FOSUserEvents; |
619cc453 | 8 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
be417ef2 | 9 | use Symfony\Component\HttpFoundation\Session\Session; |
fcb1fba5 NL |
10 | use 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 | */ | |
16 | class 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; |
24879db1 JB |
24 | private $actionMarkAsRead; |
25 | private $listMode; | |
be417ef2 | 26 | private $session; |
fcb1fba5 | 27 | |
be417ef2 | 28 | public function __construct(EntityManager $em, $theme, $itemsOnPage, $rssLimit, $language, $readingSpeed, $actionMarkAsRead, $listMode, Session $session) |
fcb1fba5 | 29 | { |
fcb1fba5 | 30 | $this->em = $em; |
772d8c4b JB |
31 | $this->theme = $theme; |
32 | $this->itemsOnPage = $itemsOnPage; | |
33 | $this->rssLimit = $rssLimit; | |
34 | $this->language = $language; | |
ca17abce | 35 | $this->readingSpeed = $readingSpeed; |
24879db1 JB |
36 | $this->actionMarkAsRead = $actionMarkAsRead; |
37 | $this->listMode = $listMode; | |
be417ef2 | 38 | $this->session = $session; |
fcb1fba5 NL |
39 | } |
40 | ||
41 | public static function getSubscribedEvents() | |
42 | { | |
4094ea47 | 43 | return [ |
ca17abce JB |
44 | // when a user register using the normal form |
45 | FOSUserEvents::REGISTRATION_COMPLETED => 'createConfig', | |
46 | // when we manually create a user using the command line | |
47 | // OR when we create it from the config UI | |
48 | FOSUserEvents::USER_CREATED => 'createConfig', | |
4094ea47 | 49 | ]; |
fcb1fba5 NL |
50 | } |
51 | ||
10bf812a | 52 | public function createConfig(UserEvent $event) |
fcb1fba5 | 53 | { |
fcb1fba5 | 54 | $config = new Config($event->getUser()); |
772d8c4b JB |
55 | $config->setTheme($this->theme); |
56 | $config->setItemsPerPage($this->itemsOnPage); | |
57 | $config->setRssLimit($this->rssLimit); | |
be417ef2 | 58 | $config->setLanguage($this->session->get('_locale', $this->language)); |
ca17abce | 59 | $config->setReadingSpeed($this->readingSpeed); |
24879db1 JB |
60 | $config->setActionMarkAsRead($this->actionMarkAsRead); |
61 | $config->setListMode($this->listMode); | |
ca17abce | 62 | |
fcb1fba5 NL |
63 | $this->em->persist($config); |
64 | $this->em->flush(); | |
65 | } | |
66 | } |