|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of AppName. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Monofony |
|
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
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace App\EventListener; |
|
15
|
|
|
|
|
16
|
|
|
use Doctrine\Common\Persistence\ObjectManager; |
|
17
|
|
|
use Sylius\Bundle\UserBundle\UserEvents; |
|
18
|
|
|
use Sylius\Component\Customer\Model\CustomerInterface; |
|
19
|
|
|
use Sylius\Component\User\Model\UserInterface; |
|
20
|
|
|
use Sylius\Component\User\Security\Generator\GeneratorInterface; |
|
21
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
22
|
|
|
use Symfony\Component\EventDispatcher\GenericEvent; |
|
23
|
|
|
use Webmozart\Assert\Assert; |
|
24
|
|
|
|
|
25
|
|
|
final class UserRegistrationListener |
|
26
|
|
|
{ |
|
27
|
|
|
/** |
|
28
|
|
|
* @var ObjectManager |
|
29
|
|
|
*/ |
|
30
|
|
|
private $userManager; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var GeneratorInterface |
|
34
|
|
|
*/ |
|
35
|
|
|
private $tokenGenerator; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @var EventDispatcherInterface |
|
39
|
|
|
*/ |
|
40
|
|
|
private $eventDispatcher; |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param ObjectManager $userManager |
|
44
|
|
|
* @param GeneratorInterface $tokenGenerator |
|
45
|
|
|
* @param EventDispatcherInterface $eventDispatcher |
|
46
|
|
|
*/ |
|
47
|
|
|
public function __construct( |
|
48
|
|
|
ObjectManager $userManager, |
|
49
|
|
|
GeneratorInterface $tokenGenerator, |
|
50
|
|
|
EventDispatcherInterface $eventDispatcher |
|
51
|
|
|
) { |
|
52
|
|
|
$this->userManager = $userManager; |
|
53
|
|
|
$this->tokenGenerator = $tokenGenerator; |
|
54
|
|
|
$this->eventDispatcher = $eventDispatcher; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param GenericEvent $event |
|
59
|
|
|
*/ |
|
60
|
|
|
public function handleUserVerification(GenericEvent $event): void |
|
61
|
|
|
{ |
|
62
|
|
|
$customer = $event->getSubject(); |
|
63
|
|
|
Assert::isInstanceOf($customer, CustomerInterface::class); |
|
64
|
|
|
|
|
65
|
|
|
$user = $customer->getUser(); |
|
66
|
|
|
Assert::notNull($user); |
|
67
|
|
|
|
|
68
|
|
|
$this->sendVerificationEmail($user); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param UserInterface $user |
|
73
|
|
|
*/ |
|
74
|
|
|
private function sendVerificationEmail(UserInterface $user): void |
|
75
|
|
|
{ |
|
76
|
|
|
$token = $this->tokenGenerator->generate(); |
|
77
|
|
|
$user->setEmailVerificationToken($token); |
|
78
|
|
|
|
|
79
|
|
|
$this->userManager->persist($user); |
|
80
|
|
|
$this->userManager->flush(); |
|
81
|
|
|
|
|
82
|
|
|
$this->eventDispatcher->dispatch(UserEvents::REQUEST_VERIFICATION_TOKEN, new GenericEvent($user)); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|