Completed
Push — master ( 074f23...421643 )
by Paweł
25:11 queued 15:02
created

handleUserVerificationToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Bundle\ShopBundle\EventListener;
13
14
use Doctrine\Common\Persistence\ObjectManager;
15
use Sylius\Bundle\UserBundle\UserEvents;
16
use Sylius\Component\Core\Model\CustomerInterface;
17
use Sylius\Component\Core\Model\ShopUserInterface;
18
use Sylius\Component\User\Security\Generator\GeneratorInterface;
19
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
20
use Symfony\Component\EventDispatcher\GenericEvent;
21
use Webmozart\Assert\Assert;
22
23
/**
24
 * @author Jan Góralski <[email protected]>
25
 */
26
final class UserRegistrationListener
27
{
28
    /**
29
     * @var ObjectManager
30
     */
31
    private $userManager;
32
33
    /**
34
     * @var GeneratorInterface
35
     */
36
    private $tokenGenerator;
37
38
    /**
39
     * @var EventDispatcherInterface
40
     */
41
    private $eventDispatcher;
42
43
    /**
44
     * @param ObjectManager $userManager
45
     * @param GeneratorInterface $tokenGenerator
46
     * @param EventDispatcherInterface $eventDispatcher
47
     */
48
    public function __construct(
49
        ObjectManager $userManager,
50
        GeneratorInterface $tokenGenerator,
51
        EventDispatcherInterface $eventDispatcher
52
    ) {
53
        $this->userManager = $userManager;
54
        $this->tokenGenerator = $tokenGenerator;
55
        $this->eventDispatcher = $eventDispatcher;
56
    }
57
58
    /**
59
     * @param GenericEvent $event
60
     */
61
    public function sendVerificationEmail(GenericEvent $event)
62
    {
63
        $customer = $event->getSubject();
64
        Assert::isInstanceOf($customer, CustomerInterface::class);
65
66
        $user = $customer->getUser();
67
        Assert::notNull($user);
68
69
        $this->handleUserVerificationToken($user);
70
    }
71
72
    /**
73
     * @param ShopUserInterface $user
74
     */
75
    private function handleUserVerificationToken(ShopUserInterface $user)
76
    {
77
        $token = $this->tokenGenerator->generate();
78
        $user->setEmailVerificationToken($token);
79
80
        $this->userManager->persist($user);
81
        $this->userManager->flush();
82
83
        $this->eventDispatcher->dispatch(UserEvents::REQUEST_VERIFICATION_TOKEN, new GenericEvent($user));
84
    }
85
}
86