Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions src/Application/UseCase/Search/GetSearchResultsUseCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

declare(strict_types=1);

namespace App\Application\UseCase\Search;

use App\Application\Service\Game\ApiGameSearchService;
use App\Shared\Dto\Game\SearchResultsOutputDto;
use App\Shared\Enum\SearchParameterEnum;
use Symfony\Component\Form\FormError;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Messenger\Exception\ExceptionInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

class GetSearchResultsUseCase
{
public function __construct(
private readonly ApiGameSearchService $apiGameSearchService,
private readonly TranslatorInterface $translator,
) {
}

/**
* @throws ExceptionInterface
*/
public function execute(FormInterface $searchGamesForm): SearchResultsOutputDto
{
$results = [
'games' => [],
'total' => 0,
];
$searchQuery = '';
$hasError = false;
$errorMessage = '';

if ($searchGamesForm->isSubmitted()) {
$gameData = $searchGamesForm->get('game')->getData();
$searchQuery = is_string($gameData) ? trim($gameData) : '';

if (!$searchGamesForm->isValid()) {
$hasError = true;
$errors = [];

/**
* @var FormError $error
*/
foreach ($searchGamesForm->getErrors(true, true) as $error) {
$errors[] = $error->getMessage();
}

$errorMessage = implode(' ', $errors);
} elseif (strlen($searchQuery) < SearchParameterEnum::MIN_SEARCH_LENGTH->value) {
$hasError = true;
$errorMessage = $this->translator->trans('form.errorMessage', ['minLength' => SearchParameterEnum::MIN_SEARCH_LENGTH->value], 'search');
} else {
$results = $this->apiGameSearchService->searchGames($searchQuery);
}
}

return new SearchResultsOutputDto(
searchQuery: $searchQuery,
results: $results,
hasError: $hasError,
errorMessage: $errorMessage,
);
}
}
43 changes: 7 additions & 36 deletions src/Presentation/Controller/SearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@

namespace App\Presentation\Controller;

use App\Application\Service\Game\ApiGameSearchService;
use App\Application\UseCase\Search\GetSearchResultsUseCase;
use App\Presentation\Form\SearchGameForm;
use App\Shared\Enum\SearchParameterEnum;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Form\FormError;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
Expand All @@ -18,46 +16,19 @@ class SearchController extends AbstractController
#[Route(path: '/search', name: 'search_games', methods: [Request::METHOD_POST])]
public function search(
Request $request,
ApiGameSearchService $apiGameSearchService,
GetSearchResultsUseCase $getSearchResultsUseCase,
): Response {
$searchGamesForm = $this->createForm(SearchGameForm::class);
$searchGamesForm->handleRequest($request);

$results = [];
$searchQuery = '';
$hasError = false;
$errorMessage = '';

if ($searchGamesForm->isSubmitted()) {
$gameData = $searchGamesForm->get('game')->getData();
$searchQuery = is_string($gameData) ? trim($gameData) : '';

if (!$searchGamesForm->isValid()) {
$hasError = true;
$errors = [];

/**
* @var FormError $error
*/
foreach ($searchGamesForm->getErrors(true, true) as $error) {
$errors[] = $error->getMessage();
}

$errorMessage = implode(' ', $errors);
} elseif (strlen($searchQuery) < SearchParameterEnum::MIN_SEARCH_LENGTH->value) {
$hasError = true;
$errorMessage = sprintf('Type at least %d characters...', SearchParameterEnum::MIN_SEARCH_LENGTH->value);
} else {
$results = $apiGameSearchService->searchGames($searchQuery);
}
}
$searchResultOutputDto = $getSearchResultsUseCase->execute($searchGamesForm);

return $this->render('@turbo/search_results_frame.html.twig', [
'searchGamesForm' => $searchGamesForm->createView(),
'games' => $results['games'] ?? [],
'searchQuery' => $searchQuery,
'hasError' => $hasError,
'errorMessage' => $errorMessage,
'games' => $searchResultOutputDto->results['games'] ?? [],
'searchQuery' => $searchResultOutputDto->searchQuery,
'hasError' => $searchResultOutputDto->hasError,
'errorMessage' => $searchResultOutputDto->errorMessage,
]);
}
}
20 changes: 20 additions & 0 deletions src/Shared/Dto/Game/SearchResultsOutputDto.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace App\Shared\Dto\Game;

use App\Domain\Model\Game\ApiGame;
use App\Domain\Model\Game\Game;

readonly class SearchResultsOutputDto
{
public function __construct(
public string $searchQuery,
/** @var array{games:array<ApiGame|Game>, total:int} */
public array $results,
public bool $hasError = false,
public string $errorMessage = '',
) {
}
}
1 change: 1 addition & 0 deletions translations/search.en.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ form:
placeholder: 'Type at least 3 characters...'
submit: 'Search'
helper: 'Search will run automatically from the third character'
errorMessage: 'Type at least %minLength% characters...'
constraints:
min_length_message: 'The search query must be at least %limit% characters'
max_length_message: 'The search query cannot exceed %limit% characters'
Expand Down