1 | <?php |
||
2 | |||
3 | namespace Locastic\Loggastic\EventSubscriber; |
||
4 | |||
5 | use Doctrine\Bundle\DoctrineBundle\EventSubscriber\EventSubscriberInterface; |
||
6 | use Doctrine\ORM\Event\PostFlushEventArgs; |
||
7 | use Doctrine\ORM\Event\PostRemoveEventArgs; |
||
8 | use Doctrine\ORM\Event\PostUpdateEventArgs; |
||
9 | use Doctrine\ORM\Event\PrePersistEventArgs; |
||
10 | use Doctrine\ORM\Event\PreRemoveEventArgs; |
||
11 | use Doctrine\ORM\Events; |
||
12 | use Locastic\Loggastic\Logger\ActivityLogger; |
||
13 | use Locastic\Loggastic\Util\ClassUtils; |
||
14 | |||
15 | final class ActivityLogDoctrineSubscriber implements EventSubscriberInterface |
||
0 ignored issues
–
show
|
|||
16 | { |
||
17 | private array $persistedEntities = []; |
||
18 | |||
19 | public function __construct( |
||
20 | private readonly ActivityLogger $activityLogger |
||
21 | ) { |
||
22 | } |
||
23 | |||
24 | public function getSubscribedEvents(): array |
||
25 | { |
||
26 | return [ |
||
27 | Events::preRemove, |
||
28 | Events::postUpdate, |
||
29 | Events::postRemove, |
||
30 | Events::prePersist, |
||
31 | Events::postFlush, |
||
32 | ]; |
||
33 | } |
||
34 | |||
35 | public function preRemove(PreRemoveEventArgs $args): void |
||
36 | { |
||
37 | $item = $args->getObject(); |
||
38 | $item->objectId = $item->getId(); |
||
39 | } |
||
40 | |||
41 | public function postRemove(PostRemoveEventArgs $args): void |
||
42 | { |
||
43 | $item = $args->getObject(); |
||
44 | |||
45 | $this->activityLogger->logDeletedItem($item, $item->objectId, ClassUtils::getClass($item)); |
||
46 | } |
||
47 | |||
48 | public function postUpdate(PostUpdateEventArgs $args): void |
||
49 | { |
||
50 | $item = $args->getObject(); |
||
51 | |||
52 | $this->activityLogger->logUpdatedItem($item); |
||
53 | } |
||
54 | |||
55 | public function prePersist(PrePersistEventArgs $args): void |
||
56 | { |
||
57 | $item = $args->getObject(); |
||
58 | |||
59 | $this->persistedEntities[] = $item; |
||
60 | } |
||
61 | |||
62 | public function postFlush(PostFlushEventArgs $args): void |
||
63 | { |
||
64 | if (empty($this->persistedEntities)) { |
||
65 | return; |
||
66 | } |
||
67 | |||
68 | foreach ($this->persistedEntities as $item) { |
||
69 | $args->getObjectManager()->refresh($item); |
||
70 | $this->activityLogger->logCreatedItem($item); |
||
71 | } |
||
72 | |||
73 | $this->persistedEntities = []; |
||
74 | } |
||
75 | } |
||
76 |
This interface has been deprecated. The supplier of the interface has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the interface will be removed and what other interface to use instead.