vendor/crehler/mojebambino-catalog/src/Subscriber/PageProductWrittenSubscriber.php line 46

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4.  * @copyright 2020 Crehler Sp. z o. o.
  5.  * @link https://crehler.com/
  6.  * @support support@crehler.com
  7.  *
  8.  * @author Mateusz FlasiƄski
  9.  *
  10.  * For the full copyright and license information, please view the LICENSE
  11.  * file that was distributed with this source code.
  12.  */
  13. namespace Crehler\Catalog\Subscriber;
  14. use Crehler\Catalog\Entity\Catalog\Aggregate\CatalogPage\ProductDotDefinition;
  15. use Shopware\Core\Framework\Context;
  16. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  17. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  18. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  19. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  20. use Shopware\Core\Framework\Struct\ArrayEntity;
  21. use Shopware\Core\Framework\Uuid\Uuid;
  22. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  23. class PageProductWrittenSubscriber implements EventSubscriberInterface
  24. {
  25.     private EntityRepositoryInterface $catalogProductDotRepository;
  26.     private array $dotsCache;
  27.     private array $positionsCache;
  28.     public function __construct(EntityRepositoryInterface $catalogProductDotRepository)
  29.     {
  30.         $this->catalogProductDotRepository $catalogProductDotRepository;
  31.         $this->dotsCache = [];
  32.         $this->positionsCache = [];
  33.     }
  34.     public static function getSubscribedEvents()
  35.     {
  36.         return ['catalog_page_product.written' => 'onProductWritten'];
  37.     }
  38.     public function onProductWritten(EntityWrittenEvent $event): void
  39.     {
  40.         $mapping = [];
  41.         $ids = [];
  42.         foreach ($event->getWriteResults() as $entityWriteResult) {
  43.             $payload $entityWriteResult->getPayload();
  44.             if (isset($payload['catalogPageId'])) {
  45.                 $catalogPageId $payload['catalogPageId'];
  46.             } else {
  47.                 $catalogPageId null;
  48.             }
  49.             if (is_string($entityWriteResult->getPrimaryKey())) {
  50.                 $ids[] = $entityWriteResult->getPrimaryKey();
  51.                 $mapping[$entityWriteResult->getPrimaryKey()] = $catalogPageId;
  52.             }
  53.             if (is_array($entityWriteResult->getPrimaryKey())) {
  54.                 $ids array_merge($entityWriteResult->getPrimaryKey(), $ids);
  55.                 foreach ($entityWriteResult->getPrimaryKey() as $id) {
  56.                     $mapping[$id] = $catalogPageId;
  57.                 }
  58.             }
  59.         }
  60.         $ids array_unique($ids);
  61.         $upsertCollection = [];
  62.         foreach ($ids as $id) {
  63.             if (isset($mapping[$id]) && !$mapping[$id] !== null) {
  64.                 $catalogPageId $mapping[$id];
  65.             } else {
  66.                 continue; // TODO Get Catalog Page Id;
  67.             }
  68.             if ($this->checkDotExists($catalogPageId$id)) {
  69.                 continue;
  70.             }
  71.             $upsertCollection[] = [
  72.                 'id' => Uuid::randomHex(),
  73.                 'catalogPageId' => $catalogPageId,
  74.                 'catalogProductId' => $id,
  75.                 'xPosition' => $this->getXPosition($catalogPageId),
  76.                 'yPosition' => ProductDotDefinition::DEFAULT_Y,
  77.                 'enabled' => false,
  78.                 'hideOnMobile' => false,
  79.                 'radius' => ProductDotDefinition::DEFAULT_RADIUS,
  80.             ];
  81.         }
  82.         if (!empty($upsertCollection)) {
  83.             $this->catalogProductDotRepository->upsert($upsertCollectionContext::createDefaultContext());
  84.         }
  85.     }
  86.     private function checkDotExists(string $catalogPageIdstring $catalogProductId): bool
  87.     {
  88.         $this->initCache($catalogPageId);
  89.         /** @var ArrayEntity $element */
  90.         foreach ($this->dotsCache[$catalogPageId] as $element) {
  91.             if ($element['catalogProductId'] === $catalogProductId) {
  92.                 return true;
  93.             }
  94.         }
  95.         return false;
  96.     }
  97.     private function initCache(string $catalogPageId): void
  98.     {
  99.         if (!isset($this->dotsCache[$catalogPageId])) {
  100.             $criteria = new Criteria();
  101.             $criteria->addFilter(new EqualsFilter('catalogPageId'$catalogPageId));
  102.             $this->dotsCache[$catalogPageId] = $this->catalogProductDotRepository->search($criteriaContext::createDefaultContext())->getElements();
  103.         }
  104.     }
  105.     private function getXPosition(string $catalogPageId): int
  106.     {
  107.         if (isset($this->positionsCache[$catalogPageId])) {
  108.             $this->positionsCache[$catalogPageId] = $this->positionsCache[$catalogPageId] + ProductDotDefinition::DEFAULT_X_STEP;
  109.             return $this->positionsCache[$catalogPageId];
  110.         }
  111.         $this->initCache($catalogPageId);
  112.         $currentX ProductDotDefinition::DEFAULT_X ProductDotDefinition::DEFAULT_X_STEP;
  113.         /** @var ArrayEntity $element */
  114.         foreach ($this->dotsCache[$catalogPageId] as $element) {
  115.             if ($element['yPosition'] === ProductDotDefinition::DEFAULT_Y && $element['xPosition'] > $currentX) {
  116.                 $currentX $element['xPosition'];
  117.             }
  118.         }
  119.         $this->positionsCache[$catalogPageId] = $currentX ProductDotDefinition::DEFAULT_X_STEP;
  120.         return $this->positionsCache[$catalogPageId];
  121.     }
  122. }