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\AbstractEntity; |
11
|
|
|
use Starkerxp\StructureBundle\Entity\TimestampInterface; |
12
|
|
|
use Starkerxp\StructureBundle\Entity\AbstractUser; |
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 $user; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var EventDispatcher |
30
|
|
|
*/ |
31
|
|
|
protected $eventDispatcher; |
32
|
|
|
|
33
|
|
|
public function __construct($user, $eventDispatcher) |
34
|
|
|
{ |
35
|
|
|
$this->user = $user; |
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 TimestampInterface )&& empty($entity->getCreatedAt())) { |
52
|
|
|
$entity->setCreatedAt(new DateTime()); |
53
|
|
|
} |
54
|
|
|
if ($entity instanceof AbstractEntity && empty($entity->getUuid())) { |
55
|
|
|
$uuid = Uuid::uuid4(); |
56
|
|
|
$entity->setUuid($uuid); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
if (!$entity instanceof AbstractUser) { |
59
|
|
|
return false; |
60
|
|
|
} |
61
|
|
|
if (empty($entity->getUser()) && $user = $this->getUser()) { |
62
|
|
|
$entity->setUser($user); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return true; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
protected function getUser() |
69
|
|
|
{ |
70
|
|
|
if (!$token = $this->user->getToken()) { |
71
|
|
|
return null; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $token->getUser(); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function onFlush(OnFlushEventArgs $event) |
78
|
|
|
{ |
79
|
|
|
$entityManager = $event->getEntityManager(); |
80
|
|
|
$uow = $entityManager->getUnitOfWork(); |
81
|
|
|
foreach ($uow->getScheduledEntityUpdates() as $entity) { |
82
|
|
|
if ($entity instanceof EntityReadOnlyInterface) { |
83
|
|
|
$uow->detach($entity); |
84
|
|
|
$this->eventDispatcher->dispatch(Events::ENTITY_READ_ONLY, new GenericEvent($entity)); |
85
|
|
|
continue; |
86
|
|
|
} |
87
|
|
|
if ($entity instanceof TimestampInterface) { |
88
|
|
|
$entity->setUpdatedAt(new DateTime()); |
89
|
|
|
} |
90
|
|
|
if ($entity instanceof AbstractEntity && empty($entity->getUuid())) { |
91
|
|
|
$uuid = Uuid::uuid4(); |
92
|
|
|
$entity->setUuid($uuid); |
|
|
|
|
93
|
|
|
} |
94
|
|
|
if (!$entity instanceof AbstractUser) { |
95
|
|
|
continue; |
96
|
|
|
} |
97
|
|
|
if (empty($entity->getUser()) && $user = $this->getUser()) { |
98
|
|
|
$entity->setUser($user); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} |
|
|
|
|
103
|
|
|
|
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: