| Total Complexity | 55 |
| Total Lines | 384 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like AdminSearchRegistry often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use AdminSearchRegistry, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 27 | #[Package('system-settings')] |
||
| 28 | #[AsMessageHandler(handles: AdminSearchIndexingMessage::class)] |
||
| 29 | class AdminSearchRegistry implements EventSubscriberInterface |
||
| 30 | { |
||
| 31 | /** |
||
| 32 | * @var array<string, mixed> |
||
| 33 | */ |
||
| 34 | private readonly array $indexer; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var array<string, mixed> |
||
| 38 | */ |
||
| 39 | private readonly array $config; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @param array<AbstractAdminIndexer>|\Traversable<AbstractAdminIndexer> $indexer |
||
| 43 | * @param array<string, mixed> $config |
||
| 44 | * @param array<string, mixed> $mapping |
||
| 45 | */ |
||
| 46 | public function __construct( |
||
| 47 | $indexer, |
||
| 48 | private readonly Connection $connection, |
||
| 49 | private readonly MessageBusInterface $queue, |
||
| 50 | private readonly EventDispatcherInterface $dispatcher, |
||
| 51 | private readonly Client $client, |
||
| 52 | private readonly AdminElasticsearchHelper $adminEsHelper, |
||
| 53 | array $config, |
||
| 54 | private readonly array $mapping |
||
| 55 | ) { |
||
| 56 | $this->indexer = ($indexer instanceof \Traversable) ? iterator_to_array($indexer) : $indexer; |
||
|
|
|||
| 57 | |||
| 58 | if (isset($config['settings']['index'])) { |
||
| 59 | if (\array_key_exists('number_of_shards', $config['settings']['index']) && $config['settings']['index']['number_of_shards'] === null) { |
||
| 60 | unset($config['settings']['index']['number_of_shards']); |
||
| 61 | } |
||
| 62 | |||
| 63 | if (\array_key_exists('number_of_replicas', $config['settings']['index']) && $config['settings']['index']['number_of_replicas'] === null) { |
||
| 64 | unset($config['settings']['index']['number_of_replicas']); |
||
| 65 | } |
||
| 66 | } |
||
| 67 | |||
| 68 | $this->config = $config; |
||
| 69 | } |
||
| 70 | |||
| 71 | public function __invoke(AdminSearchIndexingMessage $message): void |
||
| 72 | { |
||
| 73 | $indexer = $this->getIndexer($message->getEntity()); |
||
| 74 | |||
| 75 | $documents = $indexer->fetch($message->getIds()); |
||
| 76 | |||
| 77 | $this->push($indexer, $message->getIndices(), $documents, $message->getIds()); |
||
| 78 | } |
||
| 79 | |||
| 80 | public static function getSubscribedEvents(): array |
||
| 81 | { |
||
| 82 | return [ |
||
| 83 | EntityWrittenContainerEvent::class => [ |
||
| 84 | ['refresh', -1000], |
||
| 85 | ], |
||
| 86 | ]; |
||
| 87 | } |
||
| 88 | |||
| 89 | public function iterate(AdminIndexingBehavior $indexingBehavior): void |
||
| 127 | } |
||
| 128 | |||
| 129 | public function refresh(EntityWrittenContainerEvent $event): void |
||
| 130 | { |
||
| 131 | if (!$this->adminEsHelper->getEnabled() || !$this->isIndexedEntityWritten($event)) { |
||
| 132 | return; |
||
| 133 | } |
||
| 134 | |||
| 135 | if ($this->adminEsHelper->getRefreshIndices()) { |
||
| 136 | $this->refreshIndices(); |
||
| 137 | } |
||
| 138 | |||
| 139 | /** @var array<string, string> $indices */ |
||
| 140 | $indices = $this->connection->fetchAllKeyValue('SELECT `alias`, `index` FROM admin_elasticsearch_index_task'); |
||
| 141 | |||
| 142 | if (empty($indices)) { |
||
| 143 | return; |
||
| 144 | } |
||
| 145 | |||
| 146 | foreach ($this->indexer as $indexer) { |
||
| 147 | $ids = $event->getPrimaryKeys($indexer->getEntity()); |
||
| 148 | |||
| 149 | if (empty($ids)) { |
||
| 150 | continue; |
||
| 151 | } |
||
| 152 | $documents = $indexer->fetch($ids); |
||
| 153 | |||
| 154 | $this->push($indexer, $indices, $documents, $ids); |
||
| 155 | } |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @return AbstractAdminIndexer[] |
||
| 160 | */ |
||
| 161 | public function getIndexers(): iterable |
||
| 162 | { |
||
| 163 | return $this->indexer; |
||
| 164 | } |
||
| 165 | |||
| 166 | public function getIndexer(string $name): AbstractAdminIndexer |
||
| 167 | { |
||
| 168 | $indexer = $this->indexer[$name] ?? null; |
||
| 169 | if ($indexer) { |
||
| 170 | return $indexer; |
||
| 171 | } |
||
| 172 | |||
| 173 | throw new ElasticsearchIndexingException([\sprintf('Indexer for name %s not found', $name)]); |
||
| 174 | } |
||
| 175 | |||
| 176 | private function isIndexedEntityWritten(EntityWrittenContainerEvent $event): bool |
||
| 177 | { |
||
| 178 | foreach ($this->indexer as $indexer) { |
||
| 179 | $ids = $event->getPrimaryKeys($indexer->getEntity()); |
||
| 180 | |||
| 181 | if (!empty($ids)) { |
||
| 182 | return true; |
||
| 183 | } |
||
| 184 | } |
||
| 185 | |||
| 186 | return false; |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @param array<string, string> $indices |
||
| 191 | * @param array<string, array<string|int, string>> $data |
||
| 192 | * @param array<string> $ids |
||
| 193 | */ |
||
| 194 | private function push(AbstractAdminIndexer $indexer, array $indices, array $data, array $ids): void |
||
| 229 | } |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * @param array<string> $entities |
||
| 234 | * |
||
| 235 | * @throws Exception |
||
| 236 | * |
||
| 237 | * @return array<string, string> |
||
| 238 | */ |
||
| 239 | private function createIndices(array $entities): array |
||
| 240 | { |
||
| 241 | $indexTasks = []; |
||
| 242 | $indices = []; |
||
| 243 | foreach ($entities as $entityName) { |
||
| 244 | $indexer = $this->getIndexer($entityName); |
||
| 245 | $alias = $this->adminEsHelper->getIndex($indexer->getName()); |
||
| 246 | $index = $alias . '_' . time(); |
||
| 247 | |||
| 248 | if ($this->client->indices()->exists(['index' => $index])) { |
||
| 249 | continue; |
||
| 250 | } |
||
| 251 | |||
| 252 | $indices[$alias] = $index; |
||
| 253 | |||
| 254 | $this->create($indexer, $index, $alias); |
||
| 255 | |||
| 256 | $iterator = $indexer->getIterator(); |
||
| 257 | $indexTasks[] = [ |
||
| 258 | 'id' => Uuid::randomBytes(), |
||
| 259 | '`entity`' => $indexer->getEntity(), |
||
| 260 | '`index`' => $index, |
||
| 261 | '`alias`' => $alias, |
||
| 262 | '`doc_count`' => $iterator->fetchCount(), |
||
| 263 | ]; |
||
| 264 | } |
||
| 265 | |||
| 266 | $this->connection->executeStatement( |
||
| 267 | 'DELETE FROM admin_elasticsearch_index_task WHERE `entity` IN (:entities)', |
||
| 268 | ['entities' => $entities], |
||
| 269 | ['entities' => ArrayParameterType::STRING] |
||
| 270 | ); |
||
| 271 | |||
| 272 | foreach ($indexTasks as $task) { |
||
| 273 | $this->connection->insert('admin_elasticsearch_index_task', $task); |
||
| 274 | } |
||
| 275 | |||
| 276 | return $indices; |
||
| 277 | } |
||
| 278 | |||
| 279 | private function refreshIndices(): void |
||
| 313 | } |
||
| 314 | } |
||
| 315 | |||
| 316 | private function create(AbstractAdminIndexer $indexer, string $index, string $alias): void |
||
| 317 | { |
||
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * @param array<string, array<array<string, mixed>>> $result |
||
| 345 | * |
||
| 346 | * @return array<array{reason: string}|string> |
||
| 347 | */ |
||
| 348 | private function parseErrors(array $result): array |
||
| 349 | { |
||
| 350 | $errors = []; |
||
| 351 | foreach ($result['items'] as $item) { |
||
| 352 | $item = $item['index'] ?? $item['delete']; |
||
| 353 | |||
| 354 | if (\in_array($item['status'], [200, 201], true)) { |
||
| 355 | continue; |
||
| 356 | } |
||
| 357 | |||
| 358 | $errors[] = [ |
||
| 359 | 'index' => $item['_index'], |
||
| 360 | 'id' => $item['_id'], |
||
| 361 | 'type' => $item['error']['type'] ?? $item['_type'], |
||
| 362 | 'reason' => $item['error']['reason'] ?? $item['result'], |
||
| 363 | ]; |
||
| 364 | } |
||
| 365 | |||
| 366 | return $errors; |
||
| 367 | } |
||
| 368 | |||
| 369 | private function createAliasIfNotExisting(string $index, string $alias): void |
||
| 370 | { |
||
| 371 | if ($this->client->indices()->existsAlias(['name' => $alias])) { |
||
| 372 | return; |
||
| 373 | } |
||
| 374 | |||
| 375 | $this->putAlias($index, $alias); |
||
| 376 | } |
||
| 377 | |||
| 378 | /** |
||
| 379 | * @param array<string, string> $indices |
||
| 380 | */ |
||
| 381 | private function swapAlias(array $indices): void |
||
| 401 | } |
||
| 402 | } |
||
| 403 | } |
||
| 404 | |||
| 405 | private function putAlias(string $index, string $alias): void |
||
| 411 | } |
||
| 412 | } |
||
| 413 |