src/Application/UseCase/Blog/PublicListBlogArticlesHandler.php line 38

Open in your IDE?
  1. <?php
  2. namespace Whater\Application\UseCase\Blog;
  3. use Whater\Application\UseCase\AbstractCommandHandler;
  4. use Whater\Application\UseCase\Blog\CommandRequest\PublicListBlogArticlesCommand;
  5. use Whater\Domain\Blog\Model\Article;
  6. use Whater\Domain\Blog\Repository\ArticleRepositoryInterface;
  7. use Pagerfanta\Pagerfanta;
  8. use Hateoas\HateoasBuilder;
  9. /**
  10. * Class PublicListBlogArticlesHandler
  11. *
  12. * @package Whater\Application\UseCase\Blog
  13. */
  14. class PublicListBlogArticlesHandler extends AbstractCommandHandler
  15. {
  16. /**
  17. * @var ArticleRepositoryInterface
  18. */
  19. private $repositoryArticle;
  20. /**
  21. * ListBlogArticlesHandler constructor.
  22. * @param ArticleRepositoryInterface $repositoryArticle
  23. */
  24. public function __construct(ArticleRepositoryInterface $repositoryArticle)
  25. {
  26. $this->repositoryArticle = $repositoryArticle;
  27. }
  28. /**
  29. * @param PublicListBlogArticlesCommand $publicListBlogArticlesCommand
  30. * @return Pagerfanta
  31. */
  32. public function handle(PublicListBlogArticlesCommand $publicListBlogArticlesCommand): Pagerfanta
  33. {
  34. $sort = array_combine(
  35. explode(',', $publicListBlogArticlesCommand->orderParameter()) ?? [],
  36. explode(',', $publicListBlogArticlesCommand->orderValue()) ?? []
  37. );
  38. $limit = $publicListBlogArticlesCommand->limit() ?? PublicListBlogArticlesCommand::LIMIT;
  39. $page = $publicListBlogArticlesCommand->page() ?? PublicListBlogArticlesCommand::PAGE;
  40. $titleFilter = null;
  41. $categoryFilter = null;
  42. $statusFilter = Article::ARTICLE_PUBLIC;
  43. $globalFilter = null;
  44. $publishDate = new \DateTime();
  45. $showInBlog = null;
  46. $showInWhaterpoint = null;
  47. $showInDistributionNetwork = null;
  48. $filterParameters = explode(',', $publicListBlogArticlesCommand->filterParameter());
  49. $filterValues = json_decode($publicListBlogArticlesCommand->filterValue());
  50. $i = 0;
  51. foreach ($filterParameters as $filterParameter) {
  52. if ($filterParameter == "title") {
  53. $titleFilter = $filterValues[$i];
  54. } elseif ($filterParameter == "category") {
  55. $categoryFilter = $filterValues[$i];
  56. } elseif ($filterParameter == "show_in") {
  57. if ($filterValues[$i] == 'BLOG') {
  58. $showInBlog = true;
  59. } else if ($filterValues[$i] == 'WHATERPOINT') {
  60. $showInWhaterpoint = true;
  61. } else if ($filterValues[$i] == 'DISTRIBUTION_NETWORK') {
  62. $showInDistributionNetwork = true;
  63. }
  64. } elseif ($filterParameter == "tablesearch") {
  65. $globalFilter = $filterValues[$i];
  66. }
  67. $i++;
  68. }
  69. $paginatedCollection = $this->repositoryArticle->findAllPaginated(
  70. $sort,
  71. $limit,
  72. $page,
  73. $titleFilter,
  74. $statusFilter,
  75. $publishDate,
  76. $categoryFilter,
  77. $globalFilter,
  78. $showInBlog,
  79. $showInWhaterpoint,
  80. $showInDistributionNetwork
  81. );
  82. $paginatedCollection->setMaxPerPage($limit);
  83. $paginatedCollection->setCurrentPage($page);
  84. return $paginatedCollection;
  85. }
  86. }