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