vendor/dvdw/directory/src/Content/Category/SalesChannel/RandomSortCategoryListingRoute.php line 39

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace DvdwDirectory\Content\Category\SalesChannel;
  3. use Shopware\Core\Content\Category\SalesChannel\AbstractCategoryRoute;
  4. use Shopware\Core\Content\Category\SalesChannel\CategoryRouteResponse;
  5. use Shopware\Core\Content\Cms\Aggregate\CmsBlock\CmsBlockEntity;
  6. use Shopware\Core\Content\Cms\CmsPageEntity;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
  8. use Shopware\Core\Framework\Routing\Annotation\Since;
  9. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  10. use Symfony\Component\HttpFoundation\Request;
  11. use Symfony\Component\Routing\Annotation\Route;
  12. /**
  13.  * @Route(defaults={"_routeScope"={"store-api"}})
  14.  */
  15. class RandomSortCategoryListingRoute extends AbstractCategoryRoute
  16. {
  17.     private AbstractCategoryRoute $decorated;
  18.     public function __construct(
  19.         AbstractCategoryRoute $decorated
  20.     )
  21.     {
  22.         $this->decorated $decorated;
  23.     }
  24.     public function getDecorated(): AbstractCategoryRoute
  25.     {
  26.         return $this->decorated;
  27.     }
  28.     /**
  29.      * @Since("6.2.0.0")
  30.      * @Route("/store-api/category/{navigationId}", name="store-api.category.detail", methods={"GET","POST"})
  31.      */
  32.     public function load(string $navigationIdRequest $requestSalesChannelContext $context): CategoryRouteResponse
  33.     {
  34.         $response $this->getDecorated()->load($navigationId$request$context);
  35.         $page $response->getCategory()->getCmsPage();
  36.         if (($result $this->getResult($page)) === null) {
  37.             return $response;
  38.         }
  39.         $result->sort(
  40.             static function () {
  41.                 return rand(01) === ? -1;
  42.             }
  43.         );
  44.         return $response;
  45.     }
  46.     private function getResult(?CmsPageEntity $page): ?EntitySearchResult
  47.     {
  48.         if ($page === null) {
  49.             return null;
  50.         }
  51.         if (($sections $page->getSections()) === null) {
  52.             return null;
  53.         }
  54.         if (($blocks $sections->getBlocks()) === null) {
  55.             return null;
  56.         }
  57.         /** @var CmsBlockEntity|null $listingBlock */
  58.         if (($listingBlock $blocks->filter(function (CmsBlockEntity $block) {
  59.                 return $block->getType() === 'shop-directory-list';
  60.             })->first()) === null) {
  61.             return null;
  62.         }
  63.         if (($slots $listingBlock->getSlots()) === null) {
  64.             return null;
  65.         }
  66.         if (($slot $slots->getSlot('content')) === null) {
  67.             return null;
  68.         }
  69.         if (($data $slot->getData()) === null) {
  70.             return null;
  71.         }
  72.         $data $data->getVars();
  73.         if (!isset($data['listing'])) {
  74.             return null;
  75.         }
  76.         return $data['listing'];
  77.     }
  78. }