| Total Complexity | 20 |
| Total Lines | 167 |
| Duplicated Lines | 0 % |
| Changes | 3 | ||
| 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 |
||
| 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) |
||
| 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 ($this->shouldPush($owner)) { |
||
| 129 | $this->pushToElastic(); |
||
| 130 | } |
||
| 131 | |||
| 132 | if ($owner->hasField('ShowInSearch') && |
||
|
1 ignored issue
–
show
|
|||
| 133 | $owner->isChanged('ShowInSearch') && |
||
|
1 ignored issue
–
show
|
|||
| 134 | !$owner->ShowInSearch) { |
||
|
1 ignored issue
–
show
|
|||
| 135 | $this->deletedFromElastic = $this->deleteFromElastic(); |
||
| 136 | } |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * This is a separate method from the delete action, as it's a different route |
||
| 141 | * and query components. |
||
| 142 | * It can be called to add an object to the index immediately, without |
||
| 143 | * requiring a write. |
||
| 144 | * @return mixed |
||
| 145 | * @throws ClientResponseException |
||
| 146 | * @throws NotFoundExceptionInterface |
||
| 147 | * @throws ServerResponseException |
||
| 148 | */ |
||
| 149 | public function pushToElastic() |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Add ability to see what the response |
||
| 170 | * from Elasticsearch was after a delete action. |
||
| 171 | * |
||
| 172 | * @return mixed |
||
| 173 | */ |
||
| 174 | public function isDeletedFromElastic() |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Check if: |
||
| 181 | * - Owner has Versioned |
||
| 182 | * - The versioned object is published |
||
| 183 | * - The owner has the "ShowInSearch" Field |
||
| 184 | * - And if so, is it set. |
||
| 185 | * @param SiteTree|DataObjectSearchExtension|DataObjectElasticExtension|Versioned|DataObject $owner |
||
| 186 | * @return bool |
||
| 187 | */ |
||
| 188 | public function shouldPush(DataObject $owner): bool |
||
| 203 | } |
||
| 204 | } |
||
| 205 |