Total Complexity | 17 |
Total Lines | 127 |
Duplicated Lines | 0 % |
Changes | 4 | ||
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 |
||
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) |
||
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->hasField('ShowInSearch') && |
||
1 ignored issue
–
show
|
|||
128 | $this->owner->isChanged('ShowInSearch') && |
||
1 ignored issue
–
show
|
|||
129 | !$this->owner->ShowInSearch) { |
||
1 ignored issue
–
show
|
|||
130 | $this->deleteFromElastic(); |
||
131 | } |
||
132 | // @codeCoverageIgnoreEnd |
||
133 | } |
||
134 | |||
135 | /** |
||
136 | * This is a separate method from the delete action, as it's a different route |
||
137 | * and query components. |
||
138 | * It can be called to add an object to the index immediately, without |
||
139 | * requiring a write. |
||
140 | * @return array|void|bool |
||
141 | * @throws ClientResponseException |
||
142 | * @throws NotFoundExceptionInterface |
||
143 | * @throws ServerResponseException |
||
144 | */ |
||
145 | public function pushToElastic() |
||
161 | } |
||
162 | } |
||
163 |