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
|
|
|
/** |
18
|
|
|
* @return array |
19
|
|
|
*/ |
20
|
|
|
public function getSubscribedEvents() |
21
|
|
|
{ |
22
|
|
|
return [ |
23
|
|
|
Events::prePersist, |
24
|
|
|
Events::preFlush, |
25
|
|
|
Events::onClassMetadataNotFound |
26
|
|
|
]; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param LifecycleEventArgs $args |
31
|
|
|
* @throws ReadOnlyEntityCantBePersistedException |
32
|
|
|
*/ |
33
|
|
|
public function prePersist(LifecycleEventArgs $args) |
34
|
|
|
{ |
35
|
|
|
if ($this->isReadOnlyEntity($args->getObject())) { |
36
|
|
|
throw new ReadOnlyEntityCantBePersistedException($args->getObject()); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param PreFlushEventArgs $args |
42
|
|
|
* @throws ReadOnlyEntityCantBeFlushedException |
43
|
|
|
*/ |
44
|
|
|
public function preFlush(PreFlushEventArgs $args) |
45
|
|
|
{ |
46
|
|
|
$unitOfWork = $args->getEntityManager()->getUnitOfWork(); |
47
|
|
|
$entities = array_merge( |
48
|
|
|
$unitOfWork->getScheduledEntityInsertions(), |
49
|
|
|
$unitOfWork->getScheduledEntityUpdates(), |
50
|
|
|
$unitOfWork->getScheduledEntityDeletions() |
51
|
|
|
); |
52
|
|
|
foreach ($entities as $entity) { |
53
|
|
|
if ($this->isReadOnlyEntity($entity)) { |
54
|
|
|
throw new ReadOnlyEntityCantBeFlushedException($entity); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param OnClassMetadataNotFoundEventArgs $eventArgs |
61
|
|
|
*/ |
62
|
|
|
public function onClassMetadataNotFound(OnClassMetadataNotFoundEventArgs $eventArgs) |
63
|
|
|
{ |
64
|
|
|
if (class_implements( |
65
|
|
|
$eventArgs->getClassName(), |
66
|
|
|
'steevanb\\DoctrineReadOnlyHydrator\\Entity\\ReadOnlyEntityInterface' |
67
|
|
|
)) { |
68
|
|
|
$eventArgs->setFoundMetadata( |
69
|
|
|
$eventArgs->getObjectManager()->getClassMetadata(get_parent_class($eventArgs->getClassName())) |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param object $entity |
76
|
|
|
* @return bool |
77
|
|
|
*/ |
78
|
|
|
protected function isReadOnlyEntity($entity) |
79
|
|
|
{ |
80
|
|
|
return |
81
|
|
|
$entity instanceof ReadOnlyEntityInterface |
82
|
|
|
|| isset($entity->{SimpleObjectHydrator::READ_ONLY_PROPERTY}); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|