src/Application/UseCase/Whater/AppListMyWhaterPointsHandler.php line 38

Open in your IDE?
  1. <?php
  2. namespace Whater\Application\UseCase\Whater;
  3. use Whater\Application\UseCase\AbstractCommandHandler;
  4. use Whater\Application\UseCase\Whater\CommandRequest\AppListMyWhaterPointsCommand;
  5. use Whater\Domain\Whater\Repository\WhaterPointRepositoryInterface;
  6. use Pagerfanta\Pagerfanta;
  7. use Hateoas\HateoasBuilder;
  8. use Whater\Domain\User\Model\Role;
  9. /**
  10. * Class AppListMyWhaterPointsHandler
  11. *
  12. * @package Whater\Application\UseCase\Whater
  13. */
  14. class AppListMyWhaterPointsHandler extends AbstractCommandHandler
  15. {
  16. /**
  17. * @var WhaterPointRepositoryInterface
  18. */
  19. private $repositoryWhaterPoint;
  20. /**
  21. * AppListMyWhaterPointsHandler constructor.
  22. * @param WhaterPointRepositoryInterface $repositoryWhaterPoint
  23. */
  24. public function __construct(WhaterPointRepositoryInterface $repositoryWhaterPoint)
  25. {
  26. $this->repositoryWhaterPoint = $repositoryWhaterPoint;
  27. }
  28. /**
  29. * @param AppListMyWhaterPointsCommand $appListMyWhaterPointsCommand
  30. * @return Pagerfanta
  31. */
  32. public function handle(AppListMyWhaterPointsCommand $appListMyWhaterPointsCommand): Pagerfanta
  33. {
  34. //$this->logger->debug("handle appListMyWhaterPointsCommand");
  35. $sort = array_combine(
  36. explode(',', $appListMyWhaterPointsCommand->orderParameter()) ?? [],
  37. explode(',', $appListMyWhaterPointsCommand->orderValue()) ?? []
  38. );
  39. $limit = $appListMyWhaterPointsCommand->limit() ?? AppListMyWhaterPointsCommand::LIMIT;
  40. $page = $appListMyWhaterPointsCommand->page() ?? AppListMyWhaterPointsCommand::PAGE;
  41. $globalFilter = null;
  42. $hasGeopositionFilter = null;
  43. $hasDistributionNetworkFilter = null;
  44. $countryIds = null;
  45. $countryIso = null;
  46. $countryIso3 = null;
  47. $countryAreaIds = null;
  48. $townIdFilter = null;
  49. $townSlugFilter = null;
  50. $townNameFilter = null;
  51. $countryAreaNameFilter = null;
  52. $distributionNetworkNameFilter = null;
  53. $distributionNetworkIdFilter = null;
  54. $distributionNetworkTypeFilter = null;
  55. $whaterPointNameFilter = null;
  56. $whaterPointStatusFilter = null;
  57. $whaterDeviceFilter = null;
  58. $whaterpointTypeFilter = null;
  59. $validatedByAppTypeFilter = null;
  60. $userResponsableIdFilter = null;
  61. $hasProductFilter = null;
  62. $createdByIdFilter = $this->getGrantedUser()->id();
  63. // $grantUser = $appListMyWhaterPointsCommand->user();
  64. // if (!$grantUser->hasRole(Role::ROLE_ADMIN)) {
  65. // if ($grantUser->whaterOrganization() != null) {
  66. // // filtra por licencia
  67. // $countryIds = [];
  68. // $countryAreaIds = [];
  69. // $now = new \DateTime();
  70. // foreach ($grantUser->whaterOrganization()->organizationLicenses() as $organizationLicense) {
  71. // if ($organizationLicense->startDate() < $now && $organizationLicense->endDate() > $now) {
  72. // foreach ($organizationLicense->countries() as $country) {
  73. // // $this->logger->debug('countryId: ' . $country->id() . ' ' . $country->name());
  74. // array_push($countryIds, $country->id());
  75. // }
  76. // foreach ($organizationLicense->countryAreas() as $countryArea) {
  77. // // $this->logger->debug('countryAreaIds: ' . $countryArea->id() . ' ' . $countryArea->name());
  78. // array_push($countryAreaIds, $countryArea->id());
  79. // }
  80. // }
  81. // }
  82. // if (count($countryIds) == 0 && count($countryAreaIds) > 0) {
  83. // $countryIds = null;
  84. // } else if (count($countryIds) > 0 && count($countryAreaIds) == 0) {
  85. // $countryAreaIds = null;
  86. // }
  87. // } else {
  88. // //solo muestra 1 municipio de forma individual
  89. // if ($grantUser->town() != null) {
  90. // $townSlugFilter = $grantUser->town()->slug();
  91. // } else {
  92. // $townSlugFilter = 'any-town-selected';
  93. // }
  94. // }
  95. // }
  96. $filterParameters = explode(',', $appListMyWhaterPointsCommand->filterParameter());
  97. $filterValues = json_decode($appListMyWhaterPointsCommand->filterValue());
  98. $i = 0;
  99. foreach ($filterParameters as $filterParameter) {
  100. if ($filterParameter == "tablesearch") {
  101. $globalFilter = $filterValues[$i];
  102. } elseif ($filterParameter == "country_iso") {
  103. $countryIso = $filterValues[$i];
  104. } elseif ($filterParameter == "country_iso3") {
  105. $countryIso3 = $filterValues[$i];
  106. } elseif ($filterParameter == "town_slug") {
  107. $townSlugFilter = $filterValues[$i];
  108. $countryIds = null;
  109. $countryAreaIds = null;
  110. } elseif ($filterParameter == "town_uuid") {
  111. $townIdFilter = $filterValues[$i];
  112. $countryIds = null;
  113. $countryAreaIds = null;
  114. } elseif ($filterParameter == "town_name") {
  115. $townNameFilter = $filterValues[$i];
  116. } elseif ($filterParameter == "whater_point_status") {
  117. $whaterPointStatusFilter = $filterValues[$i];
  118. if ($whaterPointStatusFilter == "ALL") {
  119. $whaterPointStatusFilter = null;
  120. }
  121. } elseif ($filterParameter == "country_area_name") {
  122. $countryAreaNameFilter = $filterValues[$i];
  123. } elseif ($filterParameter == "distribution_network_uuid") {
  124. $distributionNetworkIdFilter = $filterValues[$i];
  125. } elseif ($filterParameter == "whater_point_name") {
  126. $whaterPointNameFilter = $filterValues[$i];
  127. } elseif ($filterParameter == "whater_point_type") {
  128. $whaterpointTypeFilter = $filterValues[$i];
  129. if ($whaterpointTypeFilter == "ALL") {
  130. $whaterPointStatusFilter = null;
  131. }
  132. } elseif ($filterParameter == "distribution_network_name") {
  133. $distributionNetworkNameFilter = $filterValues[$i];
  134. } elseif ($filterParameter == "has_geoposition") {
  135. $hasGeopositionFilter = $filterValues[$i] == "true" ? true : false;
  136. } elseif ($filterParameter == "whater_point_distribution_network_type") {
  137. $distributionNetworkTypeFilter = $filterValues[$i];
  138. } elseif ($filterParameter == "has_distribution_network") {
  139. $hasDistributionNetworkFilter = $filterValues[$i] == "true" ? true : false;
  140. } elseif ($filterParameter == "whater_device_uuid") {
  141. $whaterDeviceFilter = $filterValues[$i];
  142. } elseif ($filterParameter == "whater_point_validated_by_app") {
  143. $validatedByAppTypeFilter = $filterValues[$i];
  144. } elseif ($filterParameter == "has_product") {
  145. $hasProductFilter = $filterValues[$i];
  146. }
  147. $i++;
  148. }
  149. $paginatedCollection = $this->repositoryWhaterPoint->findAllPaginated(
  150. $sort,
  151. $limit,
  152. $page,
  153. $countryIds,
  154. $countryAreaIds,
  155. $countryAreaNameFilter,
  156. $countryIso,
  157. $countryIso3,
  158. null,
  159. $townIdFilter,
  160. $townSlugFilter,
  161. $townNameFilter,
  162. null,
  163. null,
  164. $whaterPointNameFilter,
  165. $whaterPointStatusFilter,
  166. null,
  167. $distributionNetworkIdFilter,
  168. $distributionNetworkNameFilter,
  169. $hasGeopositionFilter,
  170. $hasDistributionNetworkFilter,
  171. $whaterDeviceFilter,
  172. $globalFilter,
  173. null,
  174. $whaterpointTypeFilter,
  175. $distributionNetworkTypeFilter,
  176. $userResponsableIdFilter,
  177. $createdByIdFilter,
  178. $validatedByAppTypeFilter,
  179. $hasProductFilter
  180. );
  181. $paginatedCollection->setMaxPerPage($limit);
  182. $paginatedCollection->setCurrentPage($page);
  183. return $paginatedCollection;
  184. }
  185. }