- <?php
- namespace Whater\Application\UseCase\Zones;
- use Whater\Application\UseCase\AbstractCommandHandler;
- use Whater\Application\UseCase\Zones\CommandRequest\ListTownCommand;
- use Whater\Domain\Zones\Repository\TownRepositoryInterface;
- use Pagerfanta\Pagerfanta;
- use Hateoas\HateoasBuilder;
- /**
-  * Class ListTownHandler
-  *
-  * @package Whater\Application\UseCase\Zones
-  */
- class ListTownHandler extends AbstractCommandHandler
- {
-     /**
-      * @var TownRepositoryInterface
-      */
-     private $repositoryTown;
-     /**
-      * ListTownHandler constructor.
-      * @param TownRepositoryInterface $repositoryTown
-      */
-     public function __construct(TownRepositoryInterface $repositoryTown)
-     {
-         $this->repositoryTown = $repositoryTown;
-     }
-     /**
-      * @param ListTownCommand $listTownCommand
-      * @return Pagerfanta
-      */
-     public function handle(ListTownCommand $listTownCommand): Pagerfanta
-     {
-         // $this->logger->debug("handle ListTownCommand");
-         $sort      = array_combine(
-             explode(',', $listTownCommand->orderParameter()) ?? [],
-             explode(',', $listTownCommand->orderValue()) ?? []
-         );
-         $limit     = $listTownCommand->limit() ?? ListTownCommand::LIMIT;
-         $page      = $listTownCommand->page() ?? ListTownCommand::PAGE;
-         $globalFilter = null;
-         $countryiso3Filter = null;
-         $nameListFilterFilter = null;
-         $slugListFilterFilter = null;
-         $countryAreaNameListFilterFilter = null;
-         $countryNameListFilterFilter = null;
-         $hasPolygonFilter = null;
-         $isVerifiedFilter = null;
-         $whaterOrganizationFilter = null;
-         $filterParameters = explode(',', $listTownCommand->filterParameter());
-         $filterValues = json_decode($listTownCommand->filterValue());
-         $i = 0;
-         foreach ($filterParameters as $filterParameter) {
-             if ($filterParameter == "tablesearch") {
-                 $globalFilter = $filterValues[$i];
-             } else if ($filterParameter == "country_iso3") {
-                 $countryiso3Filter = $filterValues[$i];
-             } else if ($filterParameter == "town_slug") {
-                 $slugListFilterFilter = $filterValues[$i];
-             } else if ($filterParameter == "town_name") {
-                 $nameListFilterFilter = $filterValues[$i];
-             } else if ($filterParameter == "country_area_name") {
-                 $countryAreaNameListFilterFilter = $filterValues[$i];
-             } else if ($filterParameter == "country_name") {
-                 $countryNameListFilterFilter = $filterValues[$i];
-             } else if ($filterParameter == "has_polygon") {
-                 if ($filterValues[$i] == "T") {
-                     $hasPolygonFilter = true;
-                 } else {
-                     $hasPolygonFilter = false;
-                 }
-             } else if ($filterParameter == "is_verified") {
-                 $isVerifiedFilter = $filterValues[$i];
-             } else if ($filterParameter == "whaterOrganization") {
-                 $whaterOrganizationFilter = $filterValues[$i];
-             }
-             $i++;
-         }
-         $paginatedCollection = $this->repositoryTown->findAllPaginated(
-             $sort,
-             $limit,
-             $page,
-             $globalFilter,
-             $countryiso3Filter,
-             $nameListFilterFilter,
-             $slugListFilterFilter,
-             $countryAreaNameListFilterFilter,
-             $countryNameListFilterFilter,
-             $hasPolygonFilter,
-             $isVerifiedFilter,
-             $whaterOrganizationFilter
-         );
-         $paginatedCollection->setMaxPerPage($limit);
-         $paginatedCollection->setCurrentPage($page);
-         return $paginatedCollection;
-     }
- }