1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Starkerxp\StructureBundle\Listener; |
4
|
|
|
|
5
|
|
|
use DateTime; |
6
|
|
|
use Doctrine\Common\EventSubscriber; |
7
|
|
|
use Doctrine\ORM\Event\LifecycleEventArgs; |
8
|
|
|
use Doctrine\ORM\Event\OnFlushEventArgs; |
9
|
|
|
use Ramsey\Uuid\Uuid; |
10
|
|
|
use Starkerxp\StructureBundle\Entity\Entity; |
11
|
|
|
use Starkerxp\StructureBundle\Entity\TimestampEntity; |
12
|
|
|
use Starkerxp\StructureBundle\Entity\UtilisateurEntity; |
13
|
|
|
use Starkerxp\StructureBundle\Events; |
14
|
|
|
use Starkerxp\StructureBundle\Manager\EntityReadOnlyInterface; |
15
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
16
|
|
|
use Symfony\Component\EventDispatcher\GenericEvent; |
17
|
|
|
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage; |
18
|
|
|
|
19
|
|
|
// |
20
|
|
|
class EntitySubscriber implements EventSubscriber |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var TokenStorage |
25
|
|
|
*/ |
26
|
|
|
protected $utilisateur; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var EventDispatcher |
30
|
|
|
*/ |
31
|
|
|
protected $eventDispatcher; |
32
|
|
|
|
33
|
|
|
public function __construct($utilisateur, $eventDispatcher) |
34
|
|
|
{ |
35
|
|
|
$this->utilisateur = $utilisateur; |
36
|
|
|
$this->eventDispatcher = $eventDispatcher; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function getSubscribedEvents() |
40
|
|
|
{ |
41
|
|
|
return [ |
42
|
|
|
'prePersist', |
43
|
|
|
'onFlush', |
44
|
|
|
]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function prePersist(LifecycleEventArgs $args) |
48
|
|
|
{ |
49
|
|
|
$entity = $args->getEntity(); |
50
|
|
|
|
51
|
|
|
if ($entity instanceof TimestampEntity && empty($entity->getCreatedAt())) { |
52
|
|
|
$entity->setCreatedAt(new DateTime()); |
53
|
|
|
} |
54
|
|
|
if ($entity instanceof Entity && empty($entity->getUuid())) { |
55
|
|
|
$uuid = Uuid::uuid4(); |
56
|
|
|
$entity->setUuid($uuid); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
if (!$entity instanceof UtilisateurEntity) { |
59
|
|
|
return false; |
60
|
|
|
} |
61
|
|
|
if (empty($entity->getUtilisateur()) && $utilisateur = $this->getUtilisateur()) { |
62
|
|
|
$entity->setUtilisateur($utilisateur); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
protected function getUtilisateur() |
67
|
|
|
{ |
68
|
|
|
if (!$token = $this->utilisateur->getToken()) { |
69
|
|
|
return null; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $token->getUser(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function onFlush(OnFlushEventArgs $event) |
76
|
|
|
{ |
77
|
|
|
$entityManager = $event->getEntityManager(); |
78
|
|
|
$uow = $entityManager->getUnitOfWork(); |
79
|
|
|
foreach ($uow->getScheduledEntityUpdates() as $entity) { |
80
|
|
|
if ($entity instanceof EntityReadOnlyInterface) { |
81
|
|
|
$uow->detach($entity); |
82
|
|
|
$this->eventDispatcher->dispatch(Events::ENTITY_READ_ONLY, new GenericEvent($entity)); |
83
|
|
|
continue; |
84
|
|
|
} |
85
|
|
|
if ($entity instanceof TimestampEntity) { |
86
|
|
|
$entity->setUpdatedAt(new DateTime()); |
87
|
|
|
} |
88
|
|
|
if ($entity instanceof Entity && empty($entity->getUuid())) { |
89
|
|
|
$uuid = Uuid::uuid4(); |
90
|
|
|
$entity->setUuid($uuid); |
|
|
|
|
91
|
|
|
} |
92
|
|
|
if (!$entity instanceof UtilisateurEntity) { |
93
|
|
|
continue; |
94
|
|
|
} |
95
|
|
|
if (empty($entity->getUtilisateur()) && $utilisateur = $this->getUtilisateur()) { |
96
|
|
|
$entity->setUtilisateur($utilisateur); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
} |
|
|
|
|
101
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: