aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/FederationBundle/EventListener/CreateAccountListener.php
blob: 92626b15fdcd44ac5652d55f56efcbef8c16cf37 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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();
    }
}