Make Index a separate class.

This commit is contained in:
Buster Neece 2023-01-27 03:30:32 -06:00
parent 97c8b32d66
commit 0bf0bcdb02
No known key found for this signature in database
GPG Key ID: F1D2E64A0005E80E
2 changed files with 79 additions and 39 deletions

View File

@ -4,9 +4,10 @@ declare(strict_types=1);
namespace App\Service;
use App\Entity\Repository\CustomFieldRepository;
use App\Entity\StorageLocation;
use App\Environment;
use App\Service\Meilisearch\Index;
use DI\FactoryInterface;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\Psr7\HttpFactory;
use Meilisearch\Client;
@ -18,7 +19,7 @@ final class Meilisearch
public function __construct(
private readonly Environment $environment,
private readonly GuzzleClient $httpClient,
private readonly CustomFieldRepository $customFieldRepo,
private readonly FactoryInterface $factory
) {
}
@ -49,47 +50,20 @@ final class Meilisearch
return $client;
}
public function setupIndex(StorageLocation $storageLocation): void
public function getIndex(StorageLocation $storageLocation): Index
{
$indexSettings = [
'primaryKey' => 'id',
'filterableAttributes' => [
'playlists',
'is_requestable',
'is_on_demand'
],
'sortableAttributes' => [
'path',
'mtime',
'length',
'title',
'artist',
'album',
'genre',
'isrc',
],
];
foreach($this->customFieldRepo->getFieldIds() as $fieldId => $fieldShortCode) {
$indexSettings['sortableAttributes'][] = 'custom_field_'.$fieldId;
}
$client = $this->getClient();
$client->updateIndex(
self::getIndexId($storageLocation),
$indexSettings
return $this->factory->make(
Index::class,
[
'storageLocation' => $storageLocation,
'indexUid' => self::getIndexUid($storageLocation),
'client' => $this->getClient(),
]
);
}
public function addToIndex(
StorageLocation $storageLocation,
array $ids
): void {
}
public static function getIndexId(StorageLocation $storageLocation): string
public static function getIndexUid(StorageLocation $storageLocation): string
{
return 'media_'.$storageLocation->getIdRequired();
return 'media_' . $storageLocation->getIdRequired();
}
}

View File

@ -0,0 +1,66 @@
<?php
declare(strict_types=1);
namespace App\Service\Meilisearch;
use App\Doctrine\ReloadableEntityManagerInterface;
use App\Entity\Repository\CustomFieldRepository;
use App\Entity\Station;
use App\Entity\StorageLocation;
use Meilisearch\Client;
final class Index
{
public function __construct(
private readonly ReloadableEntityManagerInterface $em,
private readonly CustomFieldRepository $customFieldRepo,
private readonly Client $client,
private readonly StorageLocation $storageLocation,
private readonly string $indexUid
) {
}
public function configure(): void
{
$indexSettings = [
'primaryKey' => 'id',
'filterableAttributes' => [
'playlists',
'is_requestable',
'is_on_demand',
],
'sortableAttributes' => [
'path',
'mtime',
'length',
'title',
'artist',
'album',
'genre',
'isrc',
],
];
foreach ($this->customFieldRepo->getFieldIds() as $fieldId => $fieldShortCode) {
$indexSettings['sortableAttributes'][] = 'custom_field_' . $fieldId;
}
$this->client->updateIndex(
$this->indexUid,
$indexSettings
);
}
/** @return Station[] */
private function iterateStations(): iterable
{
return $this->em->createQuery(
<<<'DQL'
SELECT s FROM App\Entity\Station s
WHERE s.media_storage_location = :storageLocation
DQL
)->setParameter('storageLocation', $this->storageLocation)
->toIterable();
}
}