]>
Commit | Line | Data |
---|---|---|
1 | <?php | |
2 | ||
3 | namespace Wallabag\UserBundle\EventListener; | |
4 | ||
5 | use Doctrine\ORM\EntityManager; | |
6 | use FOS\UserBundle\Event\UserEvent; | |
7 | use FOS\UserBundle\FOSUserEvents; | |
8 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; | |
9 | use Symfony\Component\HttpFoundation\Session\Session; | |
10 | use 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 | */ | |
16 | class CreateConfigListener implements EventSubscriberInterface | |
17 | { | |
18 | private $em; | |
19 | private $theme; | |
20 | private $itemsOnPage; | |
21 | private $rssLimit; | |
22 | private $language; | |
23 | private $readingSpeed; | |
24 | private $actionMarkAsRead; | |
25 | private $listMode; | |
26 | private $session; | |
27 | ||
28 | public function __construct(EntityManager $em, $theme, $itemsOnPage, $rssLimit, $language, $readingSpeed, $actionMarkAsRead, $listMode, Session $session) | |
29 | { | |
30 | $this->em = $em; | |
31 | $this->theme = $theme; | |
32 | $this->itemsOnPage = $itemsOnPage; | |
33 | $this->rssLimit = $rssLimit; | |
34 | $this->language = $language; | |
35 | $this->readingSpeed = $readingSpeed; | |
36 | $this->actionMarkAsRead = $actionMarkAsRead; | |
37 | $this->listMode = $listMode; | |
38 | $this->session = $session; | |
39 | } | |
40 | ||
41 | public static function getSubscribedEvents() | |
42 | { | |
43 | return [ | |
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', | |
49 | ]; | |
50 | } | |
51 | ||
52 | public function createConfig(UserEvent $event) | |
53 | { | |
54 | $config = new Config($event->getUser()); | |
55 | $config->setTheme($this->theme); | |
56 | $config->setItemsPerPage($this->itemsOnPage); | |
57 | $config->setRssLimit($this->rssLimit); | |
58 | $config->setLanguage($this->session->get('_locale', $this->language)); | |
59 | $config->setReadingSpeed($this->readingSpeed); | |
60 | $config->setActionMarkAsRead($this->actionMarkAsRead); | |
61 | $config->setListMode($this->listMode); | |
62 | ||
63 | $this->em->persist($config); | |
64 | $this->em->flush(); | |
65 | } | |
66 | } |