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