vendor/crehler/mojebambino-engine/src/Storefront/Page/Product/ModalProductPageLoader.php line 85

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Crehler\MojeBambinoEngine\Storefront\Page\Product;
  3. use Shopware\Core\Content\Product\SalesChannel\Detail\ProductDetailRoute;
  4. use Shopware\Core\Content\Product\SalesChannel\Detail\ProductDetailRouteResponse;
  5. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  6. use Shopware\Core\Content\Property\Aggregate\PropertyGroupOption\PropertyGroupOptionCollection;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  11. use Shopware\Core\Framework\Routing\Exception\MissingRequestParameterException;
  12. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepository;
  13. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  14. use Shopware\Storefront\Page\GenericPageLoader;
  15. use Shopware\Storefront\Page\Page;
  16. use Shopware\Storefront\Page\Product\Configurator\ProductPageConfiguratorLoader;
  17. use Shopware\Storefront\Page\Product\ProductPage;
  18. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  19. use Shopware\Storefront\Page\Product\ProductPageLoader;
  20. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  21. use Symfony\Component\HttpFoundation\Request;
  22. class ModalProductPageLoader
  23. {
  24.     /**
  25.      * @var GenericPageLoader
  26.      */
  27.     private $genericLoader;
  28.     /**
  29.      * @var SalesChannelRepository
  30.      */
  31.     private $productRepository;
  32.     /**
  33.      * @var ProductPageConfiguratorLoader
  34.      */
  35.     private $configuratorLoader;
  36.     /**
  37.      * @var ProductPageLoader
  38.      */
  39.     private $productLoader;
  40.     /**
  41.      * @var EventDispatcherInterface
  42.      */
  43.     private $eventDispatcher;
  44.     public function __construct(
  45.         GenericPageLoader $genericLoader,
  46.         SalesChannelRepository $productRepository,
  47.         ProductPageConfiguratorLoader $configuratorLoader,
  48.         ProductPageLoader $productLoader,
  49.         EventDispatcherInterface $eventDispatcher
  50.     ) {
  51.         $this->genericLoader $genericLoader;
  52.         $this->productRepository $productRepository;
  53.         $this->configuratorLoader $configuratorLoader;
  54.         $this->productLoader $productLoader;
  55.         $this->eventDispatcher $eventDispatcher;
  56.     }
  57.     /**
  58.      * @param Request $request
  59.      * @param string $productId
  60.      * @param string|null $variantId
  61.      * @param SalesChannelContext $salesChannelContext
  62.      * @return ProductPage
  63.      */
  64.     public function load(Request $requeststring $productId, ?string $variantIdSalesChannelContext $salesChannelContext): ProductPage
  65.     {
  66.         $page = new Page();
  67.         $page ProductPage::createFrom($page);
  68.         if (!$productId) {
  69.             throw new MissingRequestParameterException('productId''/productId');
  70.         }
  71.         $productId $this->findBestVariant($productId$variantId$salesChannelContext);
  72.         $request->attributes->set('productId'$productId);
  73.         /** @var SalesChannelProductEntity $product */
  74.         $product $this->productLoader->load($request,$salesChannelContext)->getProduct();
  75.         $page->setProduct($product);
  76.         $page->setConfiguratorSettings(
  77.             $this->configuratorLoader->load($product$salesChannelContext)
  78.         );
  79.         $this->loadOptions($page);
  80.         $this->eventDispatcher->dispatch(
  81.             new ProductPageLoadedEvent($page$salesChannelContext$request)
  82.         );
  83.         return $page;
  84.     }
  85.     private function loadOptions(ProductPage $page): void
  86.     {
  87.         $options = new PropertyGroupOptionCollection();
  88.         $optionIds $page->getProduct()->getOptionIds();
  89.         foreach ($page->getConfiguratorSettings() as $group) {
  90.             $groupOptions $group->getOptions();
  91.             if ($groupOptions === null) {
  92.                 continue;
  93.             }
  94.             foreach ($optionIds as $optionId) {
  95.                 if ($groupOptions->has($optionId)) {
  96.                     $options->add($groupOptions->get($optionId));
  97.                 }
  98.             }
  99.         }
  100.         $page->setSelectedOptions($options);
  101.     }
  102.     /**
  103.      * @throws InconsistentCriteriaIdsException
  104.      */
  105.     private function findBestVariant(string $productId, ?string $variantIdSalesChannelContext $salesChannelContext):string
  106.     {
  107.         $criteria = (new Criteria())
  108.             ->addFilter(new EqualsFilter('product.parentId'$productId))
  109.             ->addSorting(new FieldSorting('product.price'));
  110.         if($variantId) {
  111.             $criteria
  112.                 ->addFilter(new EqualsFilter('product.id'$variantId));
  113.         }
  114.         $variantIds $this->productRepository->searchIds($criteria$salesChannelContext);
  115.         if (\count($variantIds->getIds()) > 0) {
  116.             return $variantIds->getIds()[0];
  117.         }
  118.         return $productId;
  119.     }
  120. }