wallabag/src/Wallabag/UserBundle/EventListener/CreateConfigListener.php
Jeremy Benoist 755ff9e835
Create config even if user is disabled
When a user register itself AND the wallabag instance is configured to send a confirmation email, the user is disabled when the listener (which create the config) receive the event.
There were a check (don't know why) if the user is enabled we create the config. But the user is disabled when confirmation email is actived.
2016-10-04 10:42:46 +02:00

59 lines
1.9 KiB
PHP

<?php
namespace Wallabag\UserBundle\EventListener;
use Doctrine\ORM\EntityManager;
use FOS\UserBundle\Event\UserEvent;
use FOS\UserBundle\FOSUserEvents;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Wallabag\CoreBundle\Entity\Config;
/**
* 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, $readingSpeed)
{
$this->em = $em;
$this->theme = $theme;
$this->itemsOnPage = $itemsOnPage;
$this->rssLimit = $rssLimit;
$this->language = $language;
$this->readingSpeed = $readingSpeed;
}
public static function getSubscribedEvents()
{
return [
// 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 createConfig(UserEvent $event, $eventName = null, EventDispatcherInterface $eventDispatcher = null)
{
$config = new Config($event->getUser());
$config->setTheme($this->theme);
$config->setItemsPerPage($this->itemsOnPage);
$config->setRssLimit($this->rssLimit);
$config->setLanguage($this->language);
$config->setReadingSpeed($this->readingSpeed);
$this->em->persist($config);
$this->em->flush();
}
}