Refactor MeilisearchConsistencyCommand: reduce cognitive complexity and extract constants
- Extract filterUsers() and createUserIndex() from checkUserIndex() to reduce cognitive complexity from 16 to under 15 - Add INDEXES_ENDPOINT constant to replace duplicated "/indexes" literal Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -26,6 +26,7 @@ use Symfony\Component\Console\Style\SymfonyStyle;
|
|||||||
class MeilisearchConsistencyCommand extends Command
|
class MeilisearchConsistencyCommand extends Command
|
||||||
{
|
{
|
||||||
private const INDEX_MISSING_MSG = ' [%s] Index missing';
|
private const INDEX_MISSING_MSG = ' [%s] Index missing';
|
||||||
|
private const INDEXES_ENDPOINT = '/indexes';
|
||||||
|
|
||||||
public function __construct(
|
public function __construct(
|
||||||
private MeilisearchService $meilisearch,
|
private MeilisearchService $meilisearch,
|
||||||
@@ -180,7 +181,7 @@ class MeilisearchConsistencyCommand extends Command
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!$indexExists && $fix) {
|
if (!$indexExists && $fix) {
|
||||||
$this->meilisearch->request('POST', '/indexes', ['uid' => $index, 'primaryKey' => 'id']);
|
$this->meilisearch->request('POST', self::INDEXES_ENDPOINT, ['uid' => $index, 'primaryKey' => 'id']);
|
||||||
$io->text(sprintf(' [%s] Created index, indexing %d event(s)', $index, \count($dbEvents)));
|
$io->text(sprintf(' [%s] Created index, indexing %d event(s)', $index, \count($dbEvents)));
|
||||||
foreach ($dbEvents as $event) {
|
foreach ($dbEvents as $event) {
|
||||||
$this->eventIndex->indexEvent($event);
|
$this->eventIndex->indexEvent($event);
|
||||||
@@ -223,7 +224,7 @@ class MeilisearchConsistencyCommand extends Command
|
|||||||
$dbOrders = $this->em->getRepository(BilletBuyer::class)->findBy(['event' => $event]);
|
$dbOrders = $this->em->getRepository(BilletBuyer::class)->findBy(['event' => $event]);
|
||||||
|
|
||||||
if (!$indexExists && $fix) {
|
if (!$indexExists && $fix) {
|
||||||
$this->meilisearch->request('POST', '/indexes', ['uid' => $index, 'primaryKey' => 'id']);
|
$this->meilisearch->request('POST', self::INDEXES_ENDPOINT, ['uid' => $index, 'primaryKey' => 'id']);
|
||||||
$io->text(sprintf(' [%s] Created index, indexing %d order(s)', $index, \count($dbOrders)));
|
$io->text(sprintf(' [%s] Created index, indexing %d order(s)', $index, \count($dbOrders)));
|
||||||
foreach ($dbOrders as $order) {
|
foreach ($dbOrders as $order) {
|
||||||
$this->orderIndex->indexOrder($order);
|
$this->orderIndex->indexOrder($order);
|
||||||
@@ -262,23 +263,10 @@ class MeilisearchConsistencyCommand extends Command
|
|||||||
return [0, 0];
|
return [0, 0];
|
||||||
}
|
}
|
||||||
|
|
||||||
$allUsers = $this->em->getRepository(User::class)->findAll();
|
$dbUsers = $this->filterUsers($isOrganizer);
|
||||||
|
|
||||||
if ($isOrganizer) {
|
|
||||||
$dbUsers = array_filter($allUsers, fn (User $u) => $u->isApproved() && \in_array('ROLE_ORGANIZER', $u->getRoles(), true));
|
|
||||||
} else {
|
|
||||||
$dbUsers = array_filter($allUsers, fn (User $u) => $u->isVerified() && !\in_array('ROLE_ORGANIZER', $u->getRoles(), true) && !\in_array('ROLE_ROOT', $u->getRoles(), true));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!$indexExists && $fix) {
|
if (!$indexExists && $fix) {
|
||||||
$this->meilisearch->request('POST', '/indexes', ['uid' => $index, 'primaryKey' => 'id']);
|
return $this->createUserIndex($index, $dbUsers, $isOrganizer, $io);
|
||||||
$docs = array_values(array_map(fn (User $u) => $this->userToDocument($u, $isOrganizer), $dbUsers));
|
|
||||||
$io->text(sprintf(' [%s] Created index, indexing %d user(s)', $index, \count($docs)));
|
|
||||||
if ([] !== $docs) {
|
|
||||||
$this->meilisearch->addDocuments($index, $docs);
|
|
||||||
}
|
|
||||||
|
|
||||||
return [0, \count($docs)];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$meiliIds = $this->meilisearch->getAllDocumentIds($index);
|
$meiliIds = $this->meilisearch->getAllDocumentIds($index);
|
||||||
@@ -300,6 +288,37 @@ class MeilisearchConsistencyCommand extends Command
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return list<User>
|
||||||
|
*/
|
||||||
|
private function filterUsers(bool $isOrganizer): array
|
||||||
|
{
|
||||||
|
$allUsers = $this->em->getRepository(User::class)->findAll();
|
||||||
|
|
||||||
|
if ($isOrganizer) {
|
||||||
|
return array_values(array_filter($allUsers, fn (User $u) => $u->isApproved() && \in_array('ROLE_ORGANIZER', $u->getRoles(), true)));
|
||||||
|
}
|
||||||
|
|
||||||
|
return array_values(array_filter($allUsers, fn (User $u) => $u->isVerified() && !\in_array('ROLE_ORGANIZER', $u->getRoles(), true) && !\in_array('ROLE_ROOT', $u->getRoles(), true)));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param list<User> $dbUsers
|
||||||
|
*
|
||||||
|
* @return array{int, int}
|
||||||
|
*/
|
||||||
|
private function createUserIndex(string $index, array $dbUsers, bool $isOrganizer, SymfonyStyle $io): array
|
||||||
|
{
|
||||||
|
$this->meilisearch->request('POST', self::INDEXES_ENDPOINT, ['uid' => $index, 'primaryKey' => 'id']);
|
||||||
|
$docs = array_values(array_map(fn (User $u) => $this->userToDocument($u, $isOrganizer), $dbUsers));
|
||||||
|
$io->text(sprintf(' [%s] Created index, indexing %d user(s)', $index, \count($docs)));
|
||||||
|
if ([] !== $docs) {
|
||||||
|
$this->meilisearch->addDocuments($index, $docs);
|
||||||
|
}
|
||||||
|
|
||||||
|
return [0, \count($docs)];
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return array<string, mixed>
|
* @return array<string, mixed>
|
||||||
*/
|
*/
|
||||||
|
|||||||
Reference in New Issue
Block a user