-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
SoftDeleteableListener.php
114 lines (99 loc) · 3.4 KB
/
SoftDeleteableListener.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
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
namespace Gedmo\SoftDeleteable;
use Doctrine\Common\EventArgs;
use Doctrine\ODM\MongoDB\UnitOfWork as MongoDBUnitOfWork;
use Gedmo\Mapping\MappedEventSubscriber;
/**
* SoftDeleteable listener
*
* @author Gustavo Falco <comfortablynumb84@gmail.com>
* @author Gediminas Morkevicius <gediminas.morkevicius@gmail.com>
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
class SoftDeleteableListener extends MappedEventSubscriber
{
/**
* Pre soft-delete event
*
* @var string
*/
const PRE_SOFT_DELETE = 'preSoftDelete';
/**
* Post soft-delete event
*
* @var string
*/
const POST_SOFT_DELETE = 'postSoftDelete';
/**
* {@inheritdoc}
*/
public function getSubscribedEvents()
{
return [
'loadClassMetadata',
'onFlush',
];
}
/**
* If it's a SoftDeleteable object, update the "deletedAt" field
* and skip the removal of the object
*
* @return void
*/
public function onFlush(EventArgs $args)
{
$ea = $this->getEventAdapter($args);
$om = $ea->getObjectManager();
$uow = $om->getUnitOfWork();
$evm = $om->getEventManager();
//getScheduledDocumentDeletions
foreach ($ea->getScheduledObjectDeletions($uow) as $object) {
$meta = $om->getClassMetadata(get_class($object));
$config = $this->getConfiguration($om, $meta->name);
if (isset($config['softDeleteable']) && $config['softDeleteable']) {
$reflProp = $meta->getReflectionProperty($config['fieldName']);
$oldValue = $reflProp->getValue($object);
$date = $ea->getDateValue($meta, $config['fieldName']);
// Remove `$oldValue instanceof \DateTime` check when PHP version is bumped to >=5.5
if (isset($config['hardDelete']) && $config['hardDelete'] && ($oldValue instanceof \DateTime || $oldValue instanceof \DateTimeInterface) && $oldValue <= $date) {
continue; // want to hard delete
}
$evm->dispatchEvent(
self::PRE_SOFT_DELETE,
$ea->createLifecycleEventArgsInstance($object, $om)
);
$reflProp->setValue($object, $date);
$om->persist($object);
$uow->propertyChanged($object, $config['fieldName'], $oldValue, $date);
if ($uow instanceof MongoDBUnitOfWork && !method_exists($uow, 'scheduleExtraUpdate')) {
$ea->recomputeSingleObjectChangeSet($uow, $meta, $object);
} else {
$uow->scheduleExtraUpdate($object, [
$config['fieldName'] => [$oldValue, $date],
]);
}
$evm->dispatchEvent(
self::POST_SOFT_DELETE,
$ea->createLifecycleEventArgsInstance($object, $om)
);
}
}
}
/**
* Maps additional metadata
*
* @return void
*/
public function loadClassMetadata(EventArgs $eventArgs)
{
$ea = $this->getEventAdapter($eventArgs);
$this->loadMetadataForObjectClass($ea->getObjectManager(), $eventArgs->getClassMetadata());
}
/**
* {@inheritdoc}
*/
protected function getNamespace()
{
return __NAMESPACE__;
}
}