1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Silverback\ApiComponentBundle\EventSubscriber\EntitySubscriber; |
6
|
|
|
|
7
|
|
|
use Doctrine\ORM\EntityManager; |
8
|
|
|
use Doctrine\ORM\Event\LifecycleEventArgs; |
9
|
|
|
use Doctrine\ORM\Event\PreUpdateEventArgs; |
10
|
|
|
use Doctrine\ORM\Events; |
11
|
|
|
use Silverback\ApiComponentBundle\Entity\User\User; |
12
|
|
|
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @author Daniel West <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class UserSubcriber implements EntitySubscriberInterface |
18
|
|
|
{ |
19
|
|
|
private $passwordEncoder; |
20
|
|
|
|
21
|
|
|
public function __construct( |
22
|
|
|
UserPasswordEncoderInterface $passwordEncoder |
23
|
|
|
) { |
24
|
|
|
$this->passwordEncoder = $passwordEncoder; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return array |
29
|
|
|
*/ |
30
|
|
|
public function getSubscribedEvents(): array |
31
|
|
|
{ |
32
|
|
|
return [ |
33
|
|
|
Events::prePersist => 'prePersist', |
34
|
|
|
Events::preUpdate => 'preUpdate' |
35
|
|
|
]; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function supportsEntity($entity = null): bool |
39
|
|
|
{ |
40
|
|
|
return $entity instanceof User; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function prePersist(LifecycleEventArgs $eventArgs, User $entity): void |
44
|
|
|
{ |
45
|
|
|
$this->prePersistUpdate($eventArgs->getEntityManager(), $entity); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function preUpdate(PreUpdateEventArgs $eventArgs, User $entity): void |
49
|
|
|
{ |
50
|
|
|
$this->prePersistUpdate($eventArgs->getEntityManager(), $entity); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function prePersistUpdate(EntityManager $em, User $entity): void |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
if ($entity->getPlainPassword()) { |
56
|
|
|
$password = $this->passwordEncoder->encodePassword($entity, $entity->getPlainPassword()); |
57
|
|
|
$entity->setPassword($password); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.