File size: 5,709 Bytes
cf14c12 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | <?php
declare(strict_types=1);
namespace Voice\Application\CommandHandlers;
use Ai\Domain\Services\AiServiceFactoryInterface;
use Ai\Domain\Speech\SpeechServiceInterface;
use Ai\Domain\ValueObjects\Model;
use Easy\Container\Attributes\Inject;
use Option\Application\Commands\SaveOptionCommand;
use Shared\Domain\ValueObjects\CursorDirection;
use Shared\Infrastructure\CommandBus\Dispatcher;
use Traversable;
use Voice\Application\Commands\ListVoicesCommand;
use Voice\Domain\Entities\VoiceEntity;
use Voice\Domain\Exceptions\VoiceNotFoundException;
use Voice\Domain\VoiceRepositoyInterface;
class ListVoicesCommandHandler
{
public function __construct(
private VoiceRepositoyInterface $repo,
private AiServiceFactoryInterface $factory,
private Dispatcher $dispatcher,
#[Inject('option.voice_list_fetch_date')]
private ?string $fetchedAt = null
) {}
/**
* @return Traversable<int,VoiceEntity>
* @throws VoiceNotFoundException
*/
public function handle(ListVoicesCommand $cmd): Traversable
{
$this->fetchVoices();
$cursor = $cmd->cursor
? $this->repo->ofId($cmd->cursor)
: null;
$items = $this->repo;
$models = array_filter(
$cmd->models ?: [],
fn($model) => $model instanceof Model
);
if ($models) {
$items = $items->filterByModel(...$models);
}
if ($cmd->sortDirection) {
$items = $items->sort($cmd->sortDirection, $cmd->sortParameter);
}
if ($cmd->status) {
$items = $items->filterByStatus($cmd->status);
}
if ($cmd->provider) {
$items = $items->filterByProvider($cmd->provider);
}
if ($cmd->tone) {
$items = $items->filterByTone($cmd->tone);
}
if ($cmd->useCase) {
$items = $items->filterByUseCase($cmd->useCase);
}
if ($cmd->gender) {
$items = $items->filterByGender($cmd->gender);
}
if ($cmd->accent) {
$items = $items->filterByAccent($cmd->accent);
}
if ($cmd->languageCode) {
$items = $items->filterByLanguage($cmd->languageCode);
}
if ($cmd->age) {
$items = $items->filterByAge($cmd->age);
}
if ($cmd->query) {
$items = $items->search($cmd->query);
}
if ($cmd->maxResults) {
$items = $items->setMaxResults($cmd->maxResults);
}
if ($cursor) {
if ($cmd->cursorDirection == CursorDirection::ENDING_BEFORE) {
return $items = $items->endingBefore($cursor);
}
return $items->startingAfter($cursor);
}
return $items->getIterator();
}
private function fetchVoices(): void
{
if ($this->fetchedAt && $this->fetchedAt + 3600 >= time()) {
// Voices were fetched less than an hour ago
return;
}
$news = [];
$available = [];
$providers = [];
foreach ($this->factory->list(SpeechServiceInterface::class) as $service) {
try {
foreach ($service->getVoiceList() as $newVoice) {
$key = $newVoice->getProvider()->value
. '-'
. $newVoice->getExternalId()->value;
$news[$key] = $newVoice;
if (!in_array($newVoice->getProvider()->value, $providers)) {
$providers[] = $newVoice->getProvider()->value;
}
}
} catch (\Throwable $e) {
// Failed to fetch voices from provider, continue
}
}
/** @var VoiceEntity $current */
foreach ($this->repo as $current) {
$key = $current->getProvider()->value
. '-'
. $current->getExternalId()->value;
if (
!isset($news[$key]) // Could not find voice in new list
&& in_array($current->getProvider()->value, $providers) // Fetched new voices from this provider
) {
// Voice no longer available
$this->repo->remove($current);
continue;
}
$available[$key] = $current;
}
/** @var VoiceEntity $new */
foreach ($news as $key => $new) {
if (!isset($available[$key])) {
// New voice
$this->repo->add($new);
continue;
}
$current = $available[$key];
if ($current->getUpdatedAt() !== null) {
// Voice already exists
continue;
}
// Update voice
$current->setName($new->getName());
$current->setSampleUrl($new->getSampleUrl());
$current->setTones(...$new->getTones());
$current->setUseCases(...$new->getUseCases());
$current->setGender($new->getGender());
$current->setAccent($new->getAccent());
$current->setAge($new->getAge());
$current->setSupportedLanguages(...$new->getSupportedLanguages());
}
foreach ($available as $key => $current) {
if (!isset($news[$key])) {
// Voice no longer available
$this->repo->remove($current);
}
}
// Save fetch date
$cmd = new SaveOptionCommand(
'voice_list_fetch_date',
(string) time()
);
$this->dispatcher->dispatch($cmd);
}
}
|