1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Nimble\ElasticBundle\Synchronizer; |
4
|
|
|
|
5
|
|
|
use Nimble\ElasticBundle\Document; |
6
|
|
|
use Nimble\ElasticBundle\Exception\UnexpectedTypeException; |
7
|
|
|
use Nimble\ElasticBundle\Synchronizer\Exception\InvalidSynchronizationAction; |
8
|
|
|
use Nimble\ElasticBundle\Transformer\TransformerManager; |
9
|
|
|
use Nimble\ElasticBundle\Type\Type; |
10
|
|
|
use Psr\Log\LoggerInterface; |
11
|
|
|
|
12
|
|
|
class Synchronizer implements SynchronizerInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $className; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Type |
21
|
|
|
*/ |
22
|
|
|
protected $type; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $onCreate; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $onUpdate; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $onDelete; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var TransformerManager |
41
|
|
|
*/ |
42
|
|
|
protected $transformer; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var LoggerInterface |
46
|
|
|
*/ |
47
|
|
|
protected $logger; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $className |
51
|
|
|
* @param Type $type |
52
|
|
|
* @param string $onCreate |
53
|
|
|
* @param string $onUpdate |
54
|
|
|
* @param string $onDelete |
55
|
|
|
* @param TransformerManager $transformer |
56
|
|
|
* @param LoggerInterface $logger |
57
|
|
|
*/ |
58
|
|
|
public function __construct( |
59
|
|
|
$className, |
60
|
|
|
Type $type, |
61
|
|
|
$onCreate, |
62
|
|
|
$onUpdate, |
63
|
|
|
$onDelete, |
64
|
|
|
TransformerManager $transformer, |
65
|
|
|
LoggerInterface $logger = null |
66
|
|
|
) { |
67
|
|
|
$this->className = $className; |
68
|
|
|
$this->type = $type; |
69
|
|
|
$this->onCreate = $onCreate; |
70
|
|
|
$this->onUpdate = $onUpdate; |
71
|
|
|
$this->onDelete = $onDelete; |
72
|
|
|
$this->transformer = $transformer; |
73
|
|
|
$this->logger = $logger; |
74
|
|
|
|
75
|
|
|
$this->validateAction($onCreate); |
76
|
|
|
$this->validateAction($onUpdate); |
77
|
|
|
$this->validateAction($onDelete); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @param string$action |
82
|
|
|
*/ |
83
|
|
|
protected function validateAction($action) |
84
|
|
|
{ |
85
|
|
|
if (!$action) { |
86
|
|
|
return; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if (!in_array($action, [self::ACTION_CREATE, self::ACTION_UPDATE, self::ACTION_DELETE])) { |
90
|
|
|
throw new InvalidSynchronizationAction($action); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param object $entity |
96
|
|
|
*/ |
97
|
|
|
protected function validateClass($entity) |
98
|
|
|
{ |
99
|
|
|
if (!is_a($entity, $this->className)) { |
100
|
|
|
throw new UnexpectedTypeException($entity, $this->className); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @param string $action |
106
|
|
|
* @param object $entity |
107
|
|
|
*/ |
108
|
|
|
protected function performAction($action, $entity) |
109
|
|
|
{ |
110
|
|
|
$indexId = $this->type->getIndex()->getId(); |
111
|
|
|
$typeName = $this->type->getName(); |
112
|
|
|
$ids = []; |
113
|
|
|
|
114
|
|
|
switch ($action) { |
115
|
|
|
case self::ACTION_CREATE: |
116
|
|
|
case self::ACTION_UPDATE: |
117
|
|
|
$documents = $this->transformer->transformToDocuments( |
118
|
|
|
$entity, |
119
|
|
|
$indexId, |
120
|
|
|
$typeName |
121
|
|
|
); |
122
|
|
|
|
123
|
|
|
$this->type->putDocuments($documents); |
124
|
|
|
|
125
|
|
|
if (null !== $this->logger) { |
126
|
|
|
$ids = array_map( |
127
|
|
|
function (Document $doc) { |
128
|
|
|
return $doc->getId(); |
129
|
|
|
}, |
130
|
|
|
$documents |
131
|
|
|
); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
break; |
135
|
|
|
|
136
|
|
|
case self::ACTION_DELETE: |
137
|
|
|
$ids = $this->transformer->transformToIds( |
138
|
|
|
$entity, |
139
|
|
|
$indexId, |
140
|
|
|
$typeName |
141
|
|
|
); |
142
|
|
|
|
143
|
|
|
$this->type->deleteDocuments($ids); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
if (null !== $this->logger) { |
147
|
|
|
$this->logger->info(sprintf('Performed %s synchronization for entity "%s" to "%s/%s" type, affected document ids: ["%s"].', |
148
|
|
|
strtoupper($action), |
149
|
|
|
get_class($entity), |
150
|
|
|
$indexId, |
151
|
|
|
$typeName, |
152
|
|
|
implode(', ', $ids) |
153
|
|
|
)); |
154
|
|
|
} |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* @param $entity |
159
|
|
|
*/ |
160
|
|
|
public function synchronizeCreate($entity) |
161
|
|
|
{ |
162
|
|
|
$this->validateClass($entity); |
163
|
|
|
|
164
|
|
|
if ($this->onCreate) { |
165
|
|
|
$this->performAction($this->onCreate, $entity); |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param $entity |
171
|
|
|
*/ |
172
|
|
|
public function synchronizeUpdate($entity) |
173
|
|
|
{ |
174
|
|
|
$this->validateClass($entity); |
175
|
|
|
|
176
|
|
|
if ($this->onUpdate) { |
177
|
|
|
$this->performAction($this->onUpdate, $entity); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* @param $entity |
183
|
|
|
*/ |
184
|
|
|
public function synchronizeDelete($entity) |
185
|
|
|
{ |
186
|
|
|
$this->validateClass($entity); |
187
|
|
|
|
188
|
|
|
if ($this->onDelete) { |
189
|
|
|
$this->performAction($this->onDelete, $entity); |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @return string |
195
|
|
|
*/ |
196
|
|
|
public function getClassName() |
197
|
|
|
{ |
198
|
|
|
return $this->className; |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|