<?php
namespace Whater\Application\UseCase\Zones;
use Whater\Application\UseCase\AbstractCommandHandler;
use Whater\Application\UseCase\Zones\CommandRequest\ListCountryCommand;
use Whater\Domain\Zones\Repository\CountryRepositoryInterface;
use Pagerfanta\Pagerfanta;
use Hateoas\HateoasBuilder;
/**
* Class ListCountryHandler
*
* @package Whater\Application\UseCase\Zones
*/
class ListCountryHandler extends AbstractCommandHandler
{
/**
* @var CountryRepositoryInterface
*/
private $repositoryCountry;
/**
* ListCountryHandler constructor.
* @param CountryRepositoryInterface $repositoryCountry
*/
public function __construct(CountryRepositoryInterface $repositoryCountry)
{
$this->repositoryCountry = $repositoryCountry;
}
/**
* @param ListCountryCommand $listCountryCommand
* @return Pagerfanta
*/
public function handle(ListCountryCommand $listCountryCommand): Pagerfanta
{
// $this->logger->debug("handle ListCountryCommand");
$sort = array_combine(
explode(',', $listCountryCommand->orderParameter()) ?? [],
explode(',', $listCountryCommand->orderValue()) ?? []
);
$limit = $listCountryCommand->limit() ?? ListCountryCommand::LIMIT;
$page = $listCountryCommand->page() ?? ListCountryCommand::PAGE;
$globalFilter = null;
$iso3ListFilterFilter = null;
$nameListFilterFilter = null;
$continentListFilterFilter = null;
$capitalListFilterFilter = null;
$filterParameters = explode(',', $listCountryCommand->filterParameter());
$filterValues = json_decode($listCountryCommand->filterValue());
$i = 0;
foreach ($filterParameters as $filterParameter) {
if ($filterParameter == "country_iso_3") {
$iso3ListFilterFilter = $filterValues[$i];
}else if ($filterParameter == "country_name") {
$nameListFilterFilter = $filterValues[$i];
}else if ($filterParameter == "continent") {
$continentListFilterFilter = $filterValues[$i];
}else if ($filterParameter == "capital") {
$capitalListFilterFilter = $filterValues[$i];
}else if ($filterParameter == "tablesearch") {
$globalFilter = $filterValues[$i];
}
$i++;
}
$paginatedCollection = $this->repositoryCountry->findAllPaginated(
$sort,
$limit,
$page,
$globalFilter,
$iso3ListFilterFilter,
$nameListFilterFilter,
$continentListFilterFilter,
$capitalListFilterFilter
);
$paginatedCollection->setMaxPerPage($limit);
$paginatedCollection->setCurrentPage($page);
return $paginatedCollection;
}
}