/
AlgoliaSearchDoctrineEventSubscriber.php
110 lines (95 loc) · 3.14 KB
/
AlgoliaSearchDoctrineEventSubscriber.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
<?php
namespace Algolia\AlgoliaSearchBundle\EventListener;
use Doctrine\Common\EventSubscriber;
use Doctrine\ORM\Event\OnFlushEventArgs;
use Algolia\AlgoliaSearchBundle\Indexer\Indexer;
class AlgoliaSearchDoctrineEventSubscriber implements EventSubscriber
{
private $indexer;
/**
* Under normal circumstances, the service loader will set the indexer.
* @param Indexer $indexer
*/
public function __construct(Indexer $indexer)
{
$this->indexer = $indexer;
}
/**
* The events we're interested in.
*/
public function getSubscribedEvents()
{
return array(
'onFlush',
'postFlush'
);
}
/**
* During onFlush, we tell the indexer what it should
* index or unindex right after the data has been committed to the DB.
*
* By right after, I mean during the postFlush callback.
* This is done to avoid sending wrong data to Algolia
* if the local DB rejected our changes.
*/
public function onFlush(OnFlushEventArgs $args)
{
/**
* There might have been an exception thrown during the previous flush attempt,
* because the DB rejected our changes for instance.
* We clean our indexer cache to prevent double indexing stuff if this happened.
*/
$this->indexer->removeScheduledIndexChanges();
$em = $args->getEntityManager();
$uow = $em->getUnitOfWork();
foreach ($uow->getScheduledEntityInsertions() as $entity) {
if ($this->indexer->autoIndex($entity, $em)) {
$this->create($entity);
}
}
foreach ($uow->getScheduledEntityUpdates() as $entity) {
if ($this->indexer->autoIndex($entity, $em)) {
$changeSet = $uow->getEntityChangeSet($entity);
$this->update($entity, $changeSet);
}
}
foreach ($uow->getScheduledEntityDeletions() as $entity) {
if ($this->indexer->autoIndex($entity, $em)) {
$originalData = $uow->getOriginalEntityData($entity);
$this->delete($entity, $originalData);
}
}
/**
* There are also:
*
* $uow->getScheduledCollectionDeletions();
* $uow->getScheduledCollectionUpdates();
*
* But they're not relevant here, I think.
*
* Apparently they're used for internal bookkeeping when
* doing things with Many-To-Many relationships.
*
* Leaving the comment just in case I'm wrong.
*/
}
/**
* Real work happens here, but it is delegated to the indexer.
*/
public function postFlush($ignoredArgument)
{
$this->indexer->processScheduledIndexChanges();
}
protected function create($entity)
{
$this->indexer->scheduleEntityCreation($entity);
}
protected function update($entity, $changeSet)
{
$this->indexer->scheduleEntityUpdate($entity, $changeSet);
}
protected function delete($entity, $originalData)
{
$this->indexer->scheduleEntityDeletion($entity, $originalData);
}
}