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