src/Application/UseCase/Zones/ListCountryAreaHandler.php line 37

Open in your IDE?
  1. <?php
  2. namespace Whater\Application\UseCase\Zones;
  3. use Whater\Application\UseCase\AbstractCommandHandler;
  4. use Whater\Application\UseCase\Zones\CommandRequest\ListCountryAreaCommand;
  5. use Whater\Domain\Zones\Repository\CountryAreaRepositoryInterface;
  6. use Pagerfanta\Pagerfanta;
  7. use Hateoas\HateoasBuilder;
  8. /**
  9. * Class ListCountryAreaHandler
  10. *
  11. * @package Whater\Application\UseCase\Zones
  12. */
  13. class ListCountryAreaHandler extends AbstractCommandHandler
  14. {
  15. /**
  16. * @var CountryAreaRepositoryInterface
  17. */
  18. private $repositoryCountryArea;
  19. /**
  20. * ListCountryAreaHandler constructor.
  21. * @param CountryAreaRepositoryInterface $repositoryCountryArea
  22. */
  23. public function __construct(CountryAreaRepositoryInterface $repositoryCountryArea)
  24. {
  25. $this->repositoryCountryArea = $repositoryCountryArea;
  26. }
  27. /**
  28. * @param ListCountryAreaCommand $listCountryAreaCommand
  29. * @return Pagerfanta
  30. */
  31. public function handle(ListCountryAreaCommand $listCountryAreaCommand): Pagerfanta
  32. {
  33. // $this->logger->debug("handle ListCountryAreaCommand");
  34. $sort = array_combine(
  35. explode(',', $listCountryAreaCommand->orderParameter()) ?? [],
  36. explode(',', $listCountryAreaCommand->orderValue()) ?? []
  37. );
  38. $limit = $listCountryAreaCommand->limit() ?? ListCountryAreaCommand::LIMIT;
  39. $page = $listCountryAreaCommand->page() ?? ListCountryAreaCommand::PAGE;
  40. $globalFilter = null;
  41. $nameListFilterFilter = null;
  42. $countryNameListFilterFilter = null;
  43. $continentListFilterFilter = null;
  44. $countryIso3Filter = null;
  45. $filterParameters = explode(',', $listCountryAreaCommand->filterParameter());
  46. $filterValues = json_decode($listCountryAreaCommand->filterValue());
  47. $i = 0;
  48. foreach ($filterParameters as $filterParameter) {
  49. if ($filterParameter == "country_area_name") {
  50. $nameListFilterFilter = $filterValues[$i];
  51. } else if ($filterParameter == "country_name") {
  52. $countryNameListFilterFilter = $filterValues[$i];
  53. } else if ($filterParameter == "country_iso3") {
  54. $countryIso3Filter = $filterValues[$i];
  55. } else if ($filterParameter == "continent") {
  56. $continentListFilterFilter = $filterValues[$i];
  57. } else if ($filterParameter == "tablesearch") {
  58. $globalFilter = $filterValues[$i];
  59. }
  60. $i++;
  61. }
  62. $paginatedCollection = $this->repositoryCountryArea->findAllPaginated(
  63. $sort,
  64. $limit,
  65. $page,
  66. $globalFilter,
  67. $nameListFilterFilter,
  68. $countryNameListFilterFilter,
  69. $continentListFilterFilter,
  70. $countryIso3Filter
  71. );
  72. $paginatedCollection->setMaxPerPage($limit);
  73. $paginatedCollection->setCurrentPage($page);
  74. return $paginatedCollection;
  75. }
  76. }