1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace steevanb\DoctrineReadOnlyHydrator\EventSubscriber; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\Event\OnClassMetadataNotFoundEventArgs; |
6
|
|
|
use steevanb\DoctrineReadOnlyHydrator\Hydrator\SimpleObjectHydrator; |
7
|
|
|
use Doctrine\Common\EventSubscriber; |
8
|
|
|
use Doctrine\Common\Persistence\Event\LifecycleEventArgs; |
9
|
|
|
use Doctrine\ORM\Event\PreFlushEventArgs; |
10
|
|
|
use Doctrine\ORM\Events; |
11
|
|
|
use steevanb\DoctrineReadOnlyHydrator\Entity\ReadOnlyEntityInterface; |
12
|
|
|
use steevanb\DoctrineReadOnlyHydrator\Exception\ReadOnlyEntityCantBeFlushedException; |
13
|
|
|
use steevanb\DoctrineReadOnlyHydrator\Exception\ReadOnlyEntityCantBePersistedException; |
14
|
|
|
|
15
|
|
|
class ReadOnlySubscriber implements EventSubscriber |
16
|
|
|
{ |
17
|
|
|
/** @return array */ |
18
|
|
|
public function getSubscribedEvents() |
19
|
|
|
{ |
20
|
|
|
return [ |
21
|
|
|
Events::prePersist, |
22
|
|
|
Events::preFlush, |
23
|
|
|
Events::onClassMetadataNotFound, |
24
|
|
|
Events::postLoad |
25
|
|
|
]; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param LifecycleEventArgs $args |
30
|
|
|
* @throws ReadOnlyEntityCantBePersistedException |
31
|
|
|
*/ |
32
|
|
|
public function prePersist(LifecycleEventArgs $args) |
33
|
|
|
{ |
34
|
|
|
if ($this->isReadOnlyEntity($args->getObject())) { |
35
|
|
|
throw new ReadOnlyEntityCantBePersistedException($args->getObject()); |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param PreFlushEventArgs $args |
41
|
|
|
* @throws ReadOnlyEntityCantBeFlushedException |
42
|
|
|
*/ |
43
|
|
|
public function preFlush(PreFlushEventArgs $args) |
44
|
|
|
{ |
45
|
|
|
$unitOfWork = $args->getEntityManager()->getUnitOfWork(); |
46
|
|
|
$entities = array_merge( |
47
|
|
|
$unitOfWork->getScheduledEntityInsertions(), |
48
|
|
|
$unitOfWork->getScheduledEntityUpdates(), |
49
|
|
|
$unitOfWork->getScheduledEntityDeletions() |
50
|
|
|
); |
51
|
|
|
foreach ($entities as $entity) { |
52
|
|
|
if ($this->isReadOnlyEntity($entity)) { |
53
|
|
|
throw new ReadOnlyEntityCantBeFlushedException($entity); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** @param OnClassMetadataNotFoundEventArgs $eventArgs */ |
59
|
|
|
public function onClassMetadataNotFound(OnClassMetadataNotFoundEventArgs $eventArgs) |
60
|
|
|
{ |
61
|
|
|
if (class_implements( |
62
|
|
|
$eventArgs->getClassName(), |
63
|
|
|
'steevanb\\DoctrineReadOnlyHydrator\\Entity\\ReadOnlyEntityInterface' |
64
|
|
|
)) { |
65
|
|
|
$eventArgs->setFoundMetadata( |
66
|
|
|
$eventArgs->getObjectManager()->getClassMetadata(get_parent_class($eventArgs->getClassName())) |
67
|
|
|
); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** @param LifecycleEventArgs $eventArgs */ |
72
|
|
|
public function postLoad(LifecycleEventArgs $eventArgs) |
73
|
|
|
{ |
74
|
|
|
if ($eventArgs->getObject() instanceof ReadOnlyEntityInterface) { |
75
|
|
|
// add ReadOnlyProxy to classMetada list |
76
|
|
|
// without it, you can't use Doctrine automatic id finder |
77
|
|
|
// like $queryBuilder->setParameter('foo', $foo) |
|
|
|
|
78
|
|
|
// instead of $queryBuilder->setParameter('foo', $foo->getId()) |
|
|
|
|
79
|
|
|
$eventArgs->getObjectManager()->getClassMetadata(get_class($eventArgs->getObject())); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param object $entity |
85
|
|
|
* @return bool |
86
|
|
|
*/ |
87
|
|
|
protected function isReadOnlyEntity($entity) |
88
|
|
|
{ |
89
|
|
|
return |
90
|
|
|
$entity instanceof ReadOnlyEntityInterface |
91
|
|
|
|| isset($entity->{SimpleObjectHydrator::READ_ONLY_PROPERTY}); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.