-
Notifications
You must be signed in to change notification settings - Fork 25
/
ElasticSearchWorker.php
147 lines (132 loc) · 4.5 KB
/
ElasticSearchWorker.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
<?php
namespace CoreShop2VueStorefrontBundle\Worker;
use CoreShop\Bundle\IndexBundle\Worker\AbstractWorker;
use CoreShop\Component\Index\Condition\ConditionInterface;
use CoreShop\Component\Index\Condition\ConditionRendererInterface;
use CoreShop\Component\Index\Model\IndexableInterface;
use CoreShop\Component\Index\Model\IndexColumnInterface;
use CoreShop\Component\Index\Model\IndexInterface;
use CoreShop\Component\Index\Worker\FilterGroupHelperInterface;
use CoreShop\Component\Registry\ServiceRegistryInterface;
use CoreShop2VueStorefrontBundle\Bridge\Attribute\AttributeIdGenerator;
use CoreShop2VueStorefrontBundle\Bridge\DocumentMapper\DocumentAttributeMapper;
use ONGR\ElasticsearchBundle\Service\Manager;
use Pimcore\Model\DataObject\ClassDefinition;
use Pimcore\Model\DataObject\CoreShopProduct;
class ElasticSearchWorker extends AbstractWorker
{
/** @var DocumentAttributeMapper */
private $documentAttributeBuilder;
/** @var Manager */
private $manager;
/** @var AttributeIdGenerator */
private $attributeIdGenerator;
/**
* @param ServiceRegistryInterface $extensionsRegistry
* @param ServiceRegistryInterface $getterServiceRegistry
* @param ServiceRegistryInterface $interpreterServiceRegistry
* @param FilterGroupHelperInterface $filterGroupHelper
* @param ConditionRendererInterface $conditionRenderer
* @param DocumentAttributeMapper $documentAttributeBuilder
* @param Manager $manager
* @param AttributeIdGenerator $attributeIdGenerator
*/
public function __construct(
ServiceRegistryInterface $extensionsRegistry,
ServiceRegistryInterface $getterServiceRegistry,
ServiceRegistryInterface $interpreterServiceRegistry,
FilterGroupHelperInterface $filterGroupHelper,
ConditionRendererInterface $conditionRenderer,
DocumentAttributeMapper $documentAttributeBuilder,
Manager $manager,
AttributeIdGenerator $attributeIdGenerator
) {
parent::__construct(
$extensionsRegistry,
$getterServiceRegistry,
$interpreterServiceRegistry,
$filterGroupHelper,
$conditionRenderer
);
$this->documentAttributeBuilder = $documentAttributeBuilder;
$this->manager = $manager;
$this->attributeIdGenerator = $attributeIdGenerator;
}
/**
* {@inheritdoc}
*/
public function createOrUpdateIndexStructures(IndexInterface $index)
{
try {
$this->updateAttributes($index);
} catch (\Exception $exception) {
}
}
/**
* {@inheritdoc}
*/
public function deleteIndexStructures(IndexInterface $index)
{
}
/**
* @param CoreShopProduct $index
* @param IndexableInterface $object
*/
public function deleteFromIndex(IndexInterface $index, IndexableInterface $object)
{
}
/**
* {@inheritdoc}
* @throws \ONGR\ElasticsearchBundle\Exception\BulkWithErrorsException
*/
public function updateIndex(IndexInterface $index, IndexableInterface $object)
{
$this->updateAttributes($index);
}
/**
* {@inheritdoc}
*/
public function getList(IndexInterface $index)
{
}
/**
* {@inheritdoc}
*/
public function renderCondition(ConditionInterface $condition, $prefix = null)
{
}
/**
* {@inheritdoc}
*/
public function renderFieldType($type)
{
}
/**
* @param IndexInterface $index
* @throws \ONGR\ElasticsearchBundle\Exception\BulkWithErrorsException
*/
private function updateAttributes(IndexInterface $index): void
{
$classDefinition = ClassDefinition::getByName($index->getClass());
if (!($classDefinition || $index->getColumns())) {
return;
}
/** @var IndexColumnInterface $field */
foreach ($index->getColumns() as $field) {
$fieldDefinition = $classDefinition->getFieldDefinition($field->getName());
if ($fieldDefinition) {
$fieldDefinition->id = $this->attributeIdGenerator->getId($index->getClass(), $field->getName());
$attribute = $this->documentAttributeBuilder->mapToDocument($fieldDefinition);
$this->manager->persist($attribute);
$this->manager->commit();
}
}
}
/**
* {@inheritdoc}
*/
protected function typeCastValues(IndexColumnInterface $column, $value)
{
return $value;
}
}