35 lines
1014 B
PHP
35 lines
1014 B
PHP
<?php
|
|
|
|
namespace App\Search\Action\Handler;
|
|
|
|
use App\Base\Util\ImdbMatcher;
|
|
use App\Search\Action\Result\RedirectToMediaResult;
|
|
use App\Search\Action\Result\SearchResult;
|
|
use App\Tmdb\Tmdb;
|
|
use OneToMany\RichBundle\Contract\CommandInterface;
|
|
use OneToMany\RichBundle\Contract\HandlerInterface;
|
|
use OneToMany\RichBundle\Contract\ResultInterface;
|
|
|
|
/*** @implements HandlerInterface<SearchResult> */
|
|
class SearchHandler implements HandlerInterface
|
|
{
|
|
public function __construct(
|
|
private Tmdb $tmdb,
|
|
) {}
|
|
|
|
public function handle(CommandInterface $command): ResultInterface
|
|
{
|
|
if (ImdbMatcher::isMatch($command->term)) {
|
|
$result = $this->tmdb->findByImdbId($command->term);
|
|
return new RedirectToMediaResult(
|
|
imdbId: $result->imdbId,
|
|
mediaType: $result->mediaType,
|
|
);
|
|
}
|
|
return new SearchResult(
|
|
term: $command->term,
|
|
results: $this->tmdb->search($command->term)
|
|
);
|
|
}
|
|
}
|