1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BWC\Share\Symfony\Security\User; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\EventSubscriber; |
6
|
|
|
use Doctrine\ORM\Events; |
7
|
|
|
use Doctrine\ORM\Event\LifecycleEventArgs; |
8
|
|
|
use Doctrine\ORM\Event\PreUpdateEventArgs; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Encodes account password before saving |
12
|
|
|
* Must be tagged in service definition with |
13
|
|
|
* - { name: doctrine.event_subscriber } |
14
|
|
|
*/ |
15
|
|
|
class AccountListener implements EventSubscriber |
16
|
|
|
{ |
17
|
|
|
/** @var AccountPasswordEncoderInterface */ |
18
|
|
|
private $passwordEncoder; |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
public function __construct(AccountPasswordEncoderInterface $passwordEncoder) |
22
|
|
|
{ |
23
|
|
|
$this->passwordEncoder = $passwordEncoder; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Returns an array of events this subscriber wants to listen to. |
28
|
|
|
* |
29
|
|
|
* @return array |
30
|
|
|
*/ |
31
|
|
|
public function getSubscribedEvents() |
32
|
|
|
{ |
33
|
|
|
return array( |
34
|
|
|
Events::prePersist, |
35
|
|
|
Events::preUpdate, |
36
|
|
|
); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function prePersist(LifecycleEventArgs $args) |
40
|
|
|
{ |
41
|
|
|
$this->handleEvent($args); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function preUpdate(PreUpdateEventArgs $args) |
45
|
|
|
{ |
46
|
|
|
$this->handleEvent($args); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
|
50
|
|
|
|
51
|
|
|
protected function handleEvent(LifecycleEventArgs $args) |
52
|
|
|
{ |
53
|
|
|
$entity = $args->getEntity(); |
54
|
|
|
if ($entity instanceof AdvancedUserAccountInterface) { |
55
|
|
|
$this->fixEntity($entity); |
56
|
|
|
$this->passwordEncoder->encodePassword($entity); |
57
|
|
|
if ($args instanceof PreUpdateEventArgs) { |
|
|
|
|
58
|
|
|
// We are doing a update, so we must force Doctrine to update the |
59
|
|
|
// changeset in case we changed something above |
60
|
|
|
$em = $args->getEntityManager(); |
61
|
|
|
$uow = $em->getUnitOfWork(); |
62
|
|
|
$meta = $em->getClassMetadata(get_class($entity)); |
63
|
|
|
$uow->recomputeSingleEntityChangeSet($meta, $entity); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
protected function fixEntity($entity) |
70
|
|
|
{ |
71
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
} |
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.