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