src/Application/UseCase/Zones/ListTownHandler.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\ListTownCommand;
  5. use Whater\Domain\Zones\Repository\TownRepositoryInterface;
  6. use Pagerfanta\Pagerfanta;
  7. use Hateoas\HateoasBuilder;
  8. /**
  9. * Class ListTownHandler
  10. *
  11. * @package Whater\Application\UseCase\Zones
  12. */
  13. class ListTownHandler extends AbstractCommandHandler
  14. {
  15. /**
  16. * @var TownRepositoryInterface
  17. */
  18. private $repositoryTown;
  19. /**
  20. * ListTownHandler constructor.
  21. * @param TownRepositoryInterface $repositoryTown
  22. */
  23. public function __construct(TownRepositoryInterface $repositoryTown)
  24. {
  25. $this->repositoryTown = $repositoryTown;
  26. }
  27. /**
  28. * @param ListTownCommand $listTownCommand
  29. * @return Pagerfanta
  30. */
  31. public function handle(ListTownCommand $listTownCommand): Pagerfanta
  32. {
  33. // $this->logger->debug("handle ListTownCommand");
  34. $sort = array_combine(
  35. explode(',', $listTownCommand->orderParameter()) ?? [],
  36. explode(',', $listTownCommand->orderValue()) ?? []
  37. );
  38. $limit = $listTownCommand->limit() ?? ListTownCommand::LIMIT;
  39. $page = $listTownCommand->page() ?? ListTownCommand::PAGE;
  40. $globalFilter = null;
  41. $countryiso3Filter = null;
  42. $nameListFilterFilter = null;
  43. $slugListFilterFilter = null;
  44. $countryAreaNameListFilterFilter = null;
  45. $countryNameListFilterFilter = null;
  46. $hasPolygonFilter = null;
  47. $isVerifiedFilter = null;
  48. $whaterOrganizationFilter = null;
  49. $filterParameters = explode(',', $listTownCommand->filterParameter());
  50. $filterValues = json_decode($listTownCommand->filterValue());
  51. $i = 0;
  52. foreach ($filterParameters as $filterParameter) {
  53. if ($filterParameter == "tablesearch") {
  54. $globalFilter = $filterValues[$i];
  55. } else if ($filterParameter == "country_iso3") {
  56. $countryiso3Filter = $filterValues[$i];
  57. } else if ($filterParameter == "town_slug") {
  58. $slugListFilterFilter = $filterValues[$i];
  59. } else if ($filterParameter == "town_name") {
  60. $nameListFilterFilter = $filterValues[$i];
  61. } else if ($filterParameter == "country_area_name") {
  62. $countryAreaNameListFilterFilter = $filterValues[$i];
  63. } else if ($filterParameter == "country_name") {
  64. $countryNameListFilterFilter = $filterValues[$i];
  65. } else if ($filterParameter == "has_polygon") {
  66. if ($filterValues[$i] == "T") {
  67. $hasPolygonFilter = true;
  68. } else {
  69. $hasPolygonFilter = false;
  70. }
  71. } else if ($filterParameter == "is_verified") {
  72. $isVerifiedFilter = $filterValues[$i];
  73. } else if ($filterParameter == "whaterOrganization") {
  74. $whaterOrganizationFilter = $filterValues[$i];
  75. }
  76. $i++;
  77. }
  78. $paginatedCollection = $this->repositoryTown->findAllPaginated(
  79. $sort,
  80. $limit,
  81. $page,
  82. $globalFilter,
  83. $countryiso3Filter,
  84. $nameListFilterFilter,
  85. $slugListFilterFilter,
  86. $countryAreaNameListFilterFilter,
  87. $countryNameListFilterFilter,
  88. $hasPolygonFilter,
  89. $isVerifiedFilter,
  90. $whaterOrganizationFilter
  91. );
  92. $paginatedCollection->setMaxPerPage($limit);
  93. $paginatedCollection->setCurrentPage($page);
  94. return $paginatedCollection;
  95. }
  96. }