Total Complexity | 16 |
Total Lines | 115 |
Duplicated Lines | 0 % |
Changes | 3 | ||
Bugs | 1 | Features | 0 |
1 | <?php |
||
32 | class DataObjectElasticExtension extends DataExtension |
||
33 | { |
||
34 | /** |
||
35 | * @throws NotFoundExceptionInterface |
||
36 | * @throws ValidationException |
||
37 | */ |
||
38 | public function onAfterDelete() |
||
39 | { |
||
40 | parent::onAfterDelete(); |
||
41 | $this->deleteFromElastic(); |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * Can be called directly, if a DataObject needs to be removed |
||
46 | * immediately. |
||
47 | * @return void |
||
48 | * @throws NotFoundExceptionInterface |
||
49 | */ |
||
50 | public function deleteFromElastic(): void |
||
51 | { |
||
52 | $service = new ElasticCoreService(); |
||
53 | $indexes = $service->getValidIndexes(); |
||
54 | foreach ($indexes as $index) { |
||
55 | /** @var ElasticIndex $idx */ |
||
56 | $idx = Injector::inst()->get($index); |
||
57 | $config = ElasticIndex::config()->get($idx->getIndexName()); |
||
58 | if (in_array($this->owner->ClassName, $config['Classes'])) { |
||
1 ignored issue
–
show
|
|||
59 | $deleteQuery = $this->getDeleteQuery($index); |
||
60 | $this->executeQuery($service, $deleteQuery); |
||
61 | } |
||
62 | } |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * @param mixed $index |
||
67 | * @return array |
||
68 | */ |
||
69 | private function getDeleteQuery(mixed $index): array |
||
70 | { |
||
71 | return [ |
||
72 | 'index' => $index, |
||
73 | 'body' => [ |
||
74 | 'query' => [ |
||
75 | 'match' => [ |
||
76 | 'id' => sprintf('%s-%s', $this->owner->ClassName, $this->owner->ID) |
||
2 ignored issues
–
show
|
|||
77 | ] |
||
78 | ] |
||
79 | ] |
||
80 | ]; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @throws NotFoundExceptionInterface |
||
85 | */ |
||
86 | protected function executeQuery(ElasticCoreService $service, array $deleteQuery): void |
||
87 | { |
||
88 | try { |
||
89 | $service->getClient()->deleteByQuery($deleteQuery); |
||
90 | } catch (Exception $e) { |
||
91 | // DirtyClass handling is a DataObject Search Core extension |
||
92 | $dirty = $this->owner->getDirtyClass('DELETE'); |
||
93 | $ids = json_decode($dirty->IDs ?? '[]'); |
||
94 | $ids[] = $this->owner->ID; |
||
1 ignored issue
–
show
|
|||
95 | $dirty->IDs = json_encode($ids); |
||
96 | $dirty->write(); |
||
97 | /** @var LoggerInterface $logger */ |
||
98 | $logger = Injector::inst()->get(LoggerInterface::class); |
||
99 | $logger->error($e->getMessage(), $e->getTrace()); |
||
100 | } |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * Reindex after write, if it's an indexed new/updated object |
||
105 | * @throws ClientResponseException |
||
106 | * @throws NotFoundExceptionInterface |
||
107 | * @throws ServerResponseException |
||
108 | */ |
||
109 | public function onAfterWrite() |
||
122 | } |
||
123 | // @codeCoverageIgnoreEnd |
||
124 | } |
||
125 | |||
126 | /** |
||
127 | * This is a separate method from the delete action, as it's a different route |
||
128 | * and query components. |
||
129 | * It can be called to add an object to the index immediately, without |
||
130 | * requiring a write. |
||
131 | * @throws NotFoundExceptionInterface |
||
132 | * @throws ClientResponseException |
||
133 | * @throws ServerResponseException |
||
134 | */ |
||
135 | public function doIndex() |
||
147 | } |
||
148 | } |
||
149 | } |
||
150 | } |
||
151 |