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