-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathNotificationEventListener.php
101 lines (86 loc) · 2.85 KB
/
NotificationEventListener.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php
/*
* This file is part of the merkNotificationBundle package.
*
* (c) Tim Nagel <tim@nagel.com.au>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace merk\NotificationBundle\Entity;
use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Events;
use Doctrine\ORM\Event\LifecycleEventArgs;
use Doctrine\ORM\Event\PreUpdateEventArgs;
use merk\NotificationBundle\Model\NotificationEventInterface;
use merk\NotificationBundle\Model\NotificationEventManager;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Doctrine ORM listener updating the NotificationEvent subject.
*
* @author Tim Nagel <tim@nagel.com.au>
*/
class NotificationEventListener implements EventSubscriber
{
/**
* @var NotificationEventManager
*/
private $notificationEventManager;
/**
* @var ContainerInterface
*/
private $container;
/**
* Constructor
*
* @param ContainerInterface $container
*/
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
public function getSubscribedEvents()
{
return array(
Events::prePersist,
Events::preUpdate,
Events::postLoad,
);
}
public function prePersist(LifecycleEventArgs $args)
{
$this->handlePersistEvents($args);
}
public function preUpdate(PreUpdateEventArgs $args)
{
$this->handlePersistEvents($args);
}
public function postLoad(LifecycleEventArgs $args)
{
$entity = $args->getEntity();
if ($entity instanceof NotificationEventInterface) {
if (null === $this->notificationEventManager) {
$this->notificationEventManager = $this->container->get('merk_notification.notification_event.manager');
}
$this->notificationEventManager->replaceSubject($entity);
}
}
private function handlePersistEvents(LifecycleEventArgs $args)
{
$entity = $args->getEntity();
if ($entity instanceof NotificationEventInterface) {
if (null === $this->notificationEventManager) {
$this->notificationEventManager = $this->container->get('merk_notification.notification_event.manager');
}
$this->notificationEventManager->updateSubject($entity);
if ($args instanceof PreUpdateEventArgs) {
// We are doing a update, so we must force Doctrine to update the
// changeset in case we changed something above
$em = $args->getEntityManager();
$uow = $em->getUnitOfWork();
$meta = $em->getClassMetadata(get_class($entity));
$uow->recomputeSingleEntityChangeSet($meta, $entity);
}
}
}
}