]>
Commit | Line | Data |
---|---|---|
bf6c0346 TC |
1 | <?php |
2 | ||
3 | namespace Wallabag\FederationBundle\EventListener; | |
4 | ||
5 | use Doctrine\ORM\EntityManager; | |
6 | use FOS\UserBundle\Event\UserEvent; | |
7 | use FOS\UserBundle\FOSUserEvents; | |
8 | use Symfony\Component\EventDispatcher\EventDispatcherInterface; | |
9 | use Symfony\Component\EventDispatcher\EventSubscriberInterface; | |
10 | use Wallabag\CoreBundle\Entity\Config; | |
11 | use Wallabag\FederationBundle\Entity\Account; | |
12 | ||
13 | /** | |
14 | * This listener will create the associated configuration when a user register. | |
15 | * This configuration will be created right after the registration (no matter if it needs an email validation). | |
16 | */ | |
17 | class CreateAccountListener implements EventSubscriberInterface | |
18 | { | |
19 | private $em; | |
20 | private $domainName; | |
21 | ||
22 | public function __construct(EntityManager $em, $domainName) | |
23 | { | |
24 | $this->em = $em; | |
25 | $this->domainName = $domainName; | |
26 | } | |
27 | ||
28 | public static function getSubscribedEvents() | |
29 | { | |
30 | return [ | |
31 | // when a user register using the normal form | |
32 | FOSUserEvents::REGISTRATION_COMPLETED => 'createAccount', | |
33 | // when we manually create a user using the command line | |
34 | // OR when we create it from the config UI | |
35 | FOSUserEvents::USER_CREATED => 'createAccount', | |
36 | ]; | |
37 | } | |
38 | ||
39 | public function createAccount(UserEvent $event) | |
40 | { | |
41 | $user = $event->getUser(); | |
42 | $account = new Account(); | |
43 | $account->setUser($user) | |
44 | ->setUsername($user->getUsername()) | |
45 | ->setServer($this->domainName); | |
46 | ||
47 | $this->em->persist($account); | |
48 | ||
49 | $user->setAccount($account); | |
50 | ||
51 | $this->em->persist($user); | |
52 | $this->em->flush(); | |
53 | } | |
54 | } |