- <?php
- namespace Whater\Application\UseCase\Blog;
- use Whater\Application\UseCase\AbstractCommandHandler;
- use Whater\Application\UseCase\Blog\CommandRequest\PublicListBlogArticlesCommand;
- use Whater\Domain\Blog\Model\Article;
- use Whater\Domain\Blog\Repository\ArticleRepositoryInterface;
- use Pagerfanta\Pagerfanta;
- use Hateoas\HateoasBuilder;
- /**
-  * Class PublicListBlogArticlesHandler
-  *
-  * @package Whater\Application\UseCase\Blog
-  */
- class PublicListBlogArticlesHandler extends AbstractCommandHandler
- {
-     /**
-      * @var ArticleRepositoryInterface
-      */
-     private $repositoryArticle;
-     /**
-      * ListBlogArticlesHandler constructor.
-      * @param ArticleRepositoryInterface $repositoryArticle
-      */
-     public function __construct(ArticleRepositoryInterface $repositoryArticle)
-     {
-         $this->repositoryArticle = $repositoryArticle;
-     }
-     /**
-      * @param PublicListBlogArticlesCommand $publicListBlogArticlesCommand
-      * @return Pagerfanta
-      */
-     public function handle(PublicListBlogArticlesCommand $publicListBlogArticlesCommand): Pagerfanta
-     {
-         $sort = array_combine(
-             explode(',', $publicListBlogArticlesCommand->orderParameter()) ?? [],
-             explode(',', $publicListBlogArticlesCommand->orderValue()) ?? []
-         );
-         $limit     = $publicListBlogArticlesCommand->limit() ?? PublicListBlogArticlesCommand::LIMIT;
-         $page      = $publicListBlogArticlesCommand->page() ?? PublicListBlogArticlesCommand::PAGE;
-         $titleFilter = null;
-         $categoryFilter = null;
-         $statusFilter = Article::ARTICLE_PUBLIC;
-         $globalFilter = null;
-         $publishDate = new \DateTime();
-         $showInBlog = null;
-         $showInWhaterpoint = null;
-         $showInDistributionNetwork = null;
-         $filterParameters = explode(',', $publicListBlogArticlesCommand->filterParameter());
-         $filterValues = json_decode($publicListBlogArticlesCommand->filterValue());
-         $i = 0;
-         foreach ($filterParameters as $filterParameter) {
-             if ($filterParameter == "title") {
-                 $titleFilter = $filterValues[$i];
-             } elseif ($filterParameter == "category") {
-                 $categoryFilter = $filterValues[$i];
-             } elseif ($filterParameter == "show_in") {
-                 if ($filterValues[$i] == 'BLOG') {
-                     $showInBlog = true;
-                 } else if ($filterValues[$i] == 'WHATERPOINT') {
-                     $showInWhaterpoint = true;
-                 } else if ($filterValues[$i] == 'DISTRIBUTION_NETWORK') {
-                     $showInDistributionNetwork = true;
-                 }
-             } elseif ($filterParameter == "tablesearch") {
-                 $globalFilter = $filterValues[$i];
-             }
-             $i++;
-         }
-         $paginatedCollection = $this->repositoryArticle->findAllPaginated(
-             $sort,
-             $limit,
-             $page,
-             $titleFilter,
-             $statusFilter,
-             $publishDate,
-             $categoryFilter,
-             $globalFilter,
-             $showInBlog,
-             $showInWhaterpoint,
-             $showInDistributionNetwork
-         );
-         $paginatedCollection->setMaxPerPage($limit);
-         $paginatedCollection->setCurrentPage($page);
-         return $paginatedCollection;
-     }
- }