]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - src/Wallabag/FederationBundle/EventListener/CreateAccountListener.php
WIP
[github/wallabag/wallabag.git] / src / Wallabag / FederationBundle / EventListener / CreateAccountListener.php
diff --git a/src/Wallabag/FederationBundle/EventListener/CreateAccountListener.php b/src/Wallabag/FederationBundle/EventListener/CreateAccountListener.php
new file mode 100644 (file)
index 0000000..92626b1
--- /dev/null
@@ -0,0 +1,54 @@
+<?php
+
+namespace Wallabag\FederationBundle\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;
+use Wallabag\FederationBundle\Entity\Account;
+
+/**
+ * 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 CreateAccountListener implements EventSubscriberInterface
+{
+    private $em;
+    private $domainName;
+
+    public function __construct(EntityManager $em, $domainName)
+    {
+        $this->em = $em;
+        $this->domainName = $domainName;
+    }
+
+    public static function getSubscribedEvents()
+    {
+        return [
+            // when a user register using the normal form
+            FOSUserEvents::REGISTRATION_COMPLETED => 'createAccount',
+            // when we manually create a user using the command line
+            // OR when we create it from the config UI
+            FOSUserEvents::USER_CREATED => 'createAccount',
+        ];
+    }
+
+    public function createAccount(UserEvent $event)
+    {
+        $user = $event->getUser();
+        $account = new Account();
+        $account->setUser($user)
+            ->setUsername($user->getUsername())
+            ->setServer($this->domainName);
+
+        $this->em->persist($account);
+
+        $user->setAccount($account);
+
+        $this->em->persist($user);
+        $this->em->flush();
+    }
+}