|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* (c) Lukasz D. Tulikowski <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace App\EventSubscriber; |
|
13
|
|
|
|
|
14
|
|
|
use App\Entity\User; |
|
15
|
|
|
use Doctrine\Common\EventSubscriber; |
|
16
|
|
|
use Doctrine\ORM\Event\LifecycleEventArgs; |
|
17
|
|
|
use Doctrine\ORM\Event\OnFlushEventArgs; |
|
18
|
|
|
use Doctrine\ORM\Events; |
|
19
|
|
|
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; |
|
20
|
|
|
|
|
21
|
|
|
class UserSubscriber implements EventSubscriber |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var UserPasswordEncoderInterface |
|
25
|
|
|
*/ |
|
26
|
|
|
protected $encoder; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* UserSubscriber constructor. |
|
30
|
|
|
* |
|
31
|
|
|
* @param UserPasswordEncoderInterface $encoder |
|
32
|
|
|
*/ |
|
33
|
51 |
|
public function __construct(UserPasswordEncoderInterface $encoder) |
|
34
|
|
|
{ |
|
35
|
51 |
|
$this->encoder = $encoder; |
|
36
|
51 |
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* {@inheritdoc} |
|
40
|
|
|
*/ |
|
41
|
|
|
public function getSubscribedEvents(): array |
|
42
|
|
|
{ |
|
43
|
|
|
return [ |
|
44
|
|
|
Events::prePersist, |
|
45
|
|
|
Events::postUpdate, |
|
46
|
|
|
Events::onFlush, |
|
47
|
|
|
]; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param LifecycleEventArgs $args |
|
52
|
|
|
*/ |
|
53
|
4 |
|
public function prePersist(LifecycleEventArgs $args): void |
|
54
|
|
|
{ |
|
55
|
4 |
|
$user = $args->getEntity(); |
|
56
|
|
|
|
|
57
|
4 |
|
if ($user instanceof User) { |
|
58
|
1 |
|
$this->encodePassword($user); |
|
59
|
|
|
} |
|
60
|
4 |
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param LifecycleEventArgs $args |
|
64
|
|
|
*/ |
|
65
|
4 |
|
public function postUpdate(LifecycleEventArgs $args): void |
|
66
|
|
|
{ |
|
67
|
4 |
|
$user = $args->getEntity(); |
|
68
|
|
|
|
|
69
|
4 |
|
if ($user instanceof User) { |
|
70
|
1 |
|
$this->encodePassword($user); |
|
71
|
|
|
} |
|
72
|
4 |
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param OnFlushEventArgs $eventArgs |
|
76
|
|
|
*/ |
|
77
|
12 |
|
public function onFlush(OnFlushEventArgs $eventArgs) |
|
78
|
|
|
{ |
|
79
|
12 |
|
$em = $eventArgs->getEntityManager(); |
|
80
|
12 |
|
$uow = $em->getUnitOfWork(); |
|
81
|
12 |
|
$classMetadata = $em->getClassMetadata(User::class); |
|
82
|
|
|
|
|
83
|
12 |
|
foreach ($uow->getScheduledEntityInsertions() as $entity) { |
|
84
|
4 |
|
if ($entity instanceof User) { |
|
85
|
1 |
|
$entity->setUsername($entity->getEmail()); |
|
86
|
4 |
|
$uow->recomputeSingleEntityChangeSet($classMetadata, $entity); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
12 |
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @param User $user |
|
93
|
|
|
*/ |
|
94
|
2 |
|
protected function encodePassword(User $user): void |
|
95
|
|
|
{ |
|
96
|
2 |
|
$encoded = $this->encoder->encodePassword($user, $user->getPlainPassword()); |
|
97
|
|
|
|
|
98
|
2 |
|
$user->setPassword($encoded); |
|
99
|
2 |
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|