vendor/crehler/arrangements/src/Service/ArrangementService.php line 60

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /**
  3.  * @copyright 2020 Crehler Sp. z o. o. <https://crehler.com>
  4.  *
  5.  * @author    Jakub MedyƄski <jme@crehler.com>
  6.  * @support   Crehler <support@crehler.com>
  7.  * @created   10 sie 2020
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Crehler\Arrangements\Service;
  13. use Crehler\Arrangements\Entity\Arrangement\Aggregate\ArrangementMedia\Aggregate\ArrangementMediaDot\ArrangementMediaDotCollection;
  14. use Crehler\Arrangements\Entity\Arrangement\Aggregate\ArrangementMedia\Aggregate\ArrangementMediaDot\ArrangementMediaDotEntity;
  15. use Crehler\Arrangements\Entity\Arrangement\Aggregate\ArrangementMedia\ArrangementMediaEntity;
  16. use Crehler\Arrangements\Entity\Arrangement\Aggregate\ArrangementProduct\ArrangementProductEntity;
  17. use Crehler\Arrangements\Entity\Arrangement\ArrangementEntity;
  18. use Crehler\Tools\Enum\LoaderFlags;
  19. use Crehler\Tools\Product\ProductLoaderInterface;
  20. use Monolog\Logger;
  21. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  22. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  23. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  24. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  25. use Symfony\Component\HttpFoundation\Request;
  26. class ArrangementService
  27. {
  28.     public function __construct(
  29.         private readonly EntityRepository       $arrangementRepository,
  30.         private readonly ProductLoaderInterface $productLoader,
  31.         private readonly Logger $logger
  32.     )
  33.     {
  34.     }
  35.     public function getArrangementById(string $arrangementIdSalesChannelContext $salesChannelContextRequest $request): ?ArrangementEntity
  36.     {
  37.         $criteria = new Criteria([$arrangementId]);
  38.         $criteria->addAssociation('arrangementProducts');
  39.         $criteria->addAssociation('media')
  40.             ->addAssociation('cover.arrangementMediaDots.arrangementProduct')
  41.             ->addAssociation('cover.arrangementMediaDots.arrangementProduct');
  42.         $criteria->getAssociation('media')
  43.             ->addAssociation('arrangementMediaDots.arrangementProduct')
  44.             ->addAssociation('arrangementMediaDots.arrangementProduct')
  45.             ->addSorting(new FieldSorting('position'));
  46.         /** @var ArrangementEntity $arrangement */
  47.         $arrangement $this->arrangementRepository->search($criteria$salesChannelContext->getContext())->first();
  48.         $this->filterDotCollection($arrangement->getCover()->getArrangementMediaDots(), $salesChannelContext);
  49.         /** @var ArrangementMediaEntity $media */
  50.         foreach ($arrangement->getMedia() as $media) {
  51.             $this->filterDotCollection($media->getArrangementMediaDots(), $salesChannelContext);
  52.         }
  53.         /** @var ArrangementProductEntity $arrangementProduct */
  54.         foreach ($arrangement->getArrangementProducts() as $arrangementProduct) {
  55.             $salesChannelProduct $this->productLoader->load(
  56.                 $arrangementProduct->getProductId(),
  57.                 $salesChannelContext,
  58.                 LoaderFlags::SkipPageLoading,
  59.                 LoaderFlags::SkipSeoCategory,
  60.                 LoaderFlags::IgnoreHideCloseoutProductsWhenOutOfStock)
  61.                 ->first();
  62.             if ($salesChannelProduct === null) {
  63.                 $this->logger->warning("Remove " $arrangementProduct->getProductId() . " from arrangement " $arrangement->getId());
  64.                 $arrangement->getArrangementProducts()->remove($arrangementProduct->getId());
  65.                 continue;
  66.             }
  67.             $arrangementProduct->setProduct($salesChannelProduct);
  68.         }
  69.         return $arrangement;
  70.     }
  71.     private function filterDotCollection(ArrangementMediaDotCollection $arrangementMediaDotCollectionSalesChannelContext $salesChannelContext): void
  72.     {
  73.         /** @var ArrangementMediaDotEntity $arrangementMediaDot */
  74.         foreach ($arrangementMediaDotCollection as $arrangementMediaDot) {
  75.             $productId $arrangementMediaDot->getArrangementProduct()->getProductId();
  76.             $salesChannelProduct $this->productLoader->load(
  77.                 $productId,
  78.                 $salesChannelContext,
  79.                 LoaderFlags::SkipPageLoading,
  80.                 LoaderFlags::SkipSeoCategory,
  81.                 LoaderFlags::IgnoreHideCloseoutProductsWhenOutOfStock)
  82.                 ->first();
  83.             if ($salesChannelProduct === null) {
  84.                 $arrangementMediaDotCollection->remove($arrangementMediaDot->getId());
  85.                 $this->logger->warning("Remove " $productId " from arrangement media dot " $arrangementMediaDot->getId());
  86.                 continue;
  87.             }
  88.             $arrangementMediaDot->getArrangementProduct()->setProduct($salesChannelProduct);
  89.         }
  90.     }
  91. }