-
Notifications
You must be signed in to change notification settings - Fork 14
/
AbstractImportConsumeEventListener.php
169 lines (141 loc) · 3.99 KB
/
AbstractImportConsumeEventListener.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
/*
* This file is part of the ONGR package.
*
* (c) NFQ Technologies UAB <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ONGR\ConnectionsBundle\EventListener;
use ONGR\ConnectionsBundle\Log\EventLoggerAwareTrait;
use ONGR\ConnectionsBundle\Pipeline\Event\ItemPipelineEvent;
use ONGR\ConnectionsBundle\Pipeline\Item\AbstractImportItem;
use ONGR\ElasticsearchBundle\ORM\Manager;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LogLevel;
/**
* AbstractImportConsumeEventListener - called after modify event. Puts document into Elasticsearch.
*/
abstract class AbstractImportConsumeEventListener extends AbstractConsumeEventListener implements LoggerAwareInterface
{
use EventLoggerAwareTrait;
/**
* @var Manager
*/
private $elasticsearchManager;
/**
* @var string Import item class of an item contained in ItemPipelineEvent.
*/
private $importItemClass;
/**
* @var AbstractImportItem
*/
private $importItem;
/**
* @param Manager $elasticsearchManager
* @param string $itemClass
*/
public function __construct(Manager $elasticsearchManager = null, $itemClass = null)
{
$this->elasticsearchManager = $elasticsearchManager;
$this->importItemClass = $itemClass;
}
/**
* Consume event.
*
* @param ItemPipelineEvent $event
*/
public function consume(ItemPipelineEvent $event)
{
if (!$this->setItem($event)) {
return;
}
$this->log(
sprintf(
'Start update single document of type %s id: %s',
get_class($this->getItem()->getDocument()),
$this->getItem()->getDocument()->getId()
)
);
if (!$this->persistDocument()) {
return;
};
$this->log('End an update of a single document.');
}
/**
* Persist document to Elasticsearch.
*
* @return bool
*/
protected function persistDocument()
{
$this->getElasticsearchManager()->persist($this->getItem()->getDocument());
return true;
}
/**
* Validates the class name of event item and prepares internal document for persistence operation.
*
* @param ItemPipelineEvent $event
*
* @return bool
*/
protected function setItem(ItemPipelineEvent $event)
{
/** @var AbstractImportItem $tempItem */
$tempItem = $event->getItem();
if (!$tempItem instanceof $this->importItemClass) {
$this->log("Item provided is not an {$this->importItemClass}", LogLevel::ERROR);
return false;
}
$this->importItem = $tempItem;
return true;
}
/**
* @return AbstractImportItem
*/
protected function getItem()
{
return $this->importItem;
}
/**
* @return Manager
*/
public function getElasticsearchManager()
{
if ($this->elasticsearchManager === null) {
throw new \LogicException('Elasticsearch manager must be set before using \'getElasticsearchManager\'');
}
return $this->elasticsearchManager;
}
/**
* @param Manager $elasticsearchManager
*
* @return $this
*/
public function setElasticsearchManager(Manager $elasticsearchManager)
{
$this->elasticsearchManager = $elasticsearchManager;
return $this;
}
/**
* @return string
*/
public function getImportItemClass()
{
if ($this->importItemClass === null) {
throw new \LogicException('Import item class must be set before using \'getImportItemClass\'');
}
return $this->importItemClass;
}
/**
* @param string $importItemClass
*
* @return $this
*/
public function setImportItemClass($importItemClass)
{
$this->importItemClass = $importItemClass;
return $this;
}
}