vendor/crehler/mojebambino-engine/src/Subscriber/PropertyGroupOptionSubscriber.php line 33

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Crehler\MojeBambinoEngine\Subscriber;
  3. use Crehler\MojeBambinoEngine\Entity\Property\PropertyGroupOptionIncrementEntity;
  4. use Crehler\MojeBambinoEngine\Struct\AutoIncrementStruct;
  5. use Shopware\Core\Content\Property\PropertyEvents;
  6. use Shopware\Core\Framework\Api\Context\SalesChannelApiSource;
  7. use Shopware\Core\Framework\Context;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  12. use Shopware\Core\Framework\Uuid\Uuid;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. class PropertyGroupOptionSubscriber implements EventSubscriberInterface
  15. {
  16.     private EntityRepository $productGroupOptionIncrementRepository;
  17.     public function __construct(EntityRepository $productGroupOptionIncrementRepository)
  18.     {
  19.         $this->productGroupOptionIncrementRepository $productGroupOptionIncrementRepository;
  20.     }
  21.     public static function getSubscribedEvents(): array
  22.     {
  23.         return [
  24.             PropertyEvents::PROPERTY_GROUP_OPTION_LOADED_EVENT => 'onProductGroupOptionEventLoaded'
  25.         ];
  26.     }
  27.     public function onProductGroupOptionEventLoaded(EntityLoadedEvent $event): void
  28.     {
  29.         if ($event->getContext()->getSource() instanceof SalesChannelApiSource) {
  30.             return;
  31.         }
  32.         try {
  33.             foreach ($event->getEntities() as $entity) {
  34.                 $criteria = new Criteria();
  35.                 $criteria->addFilter(
  36.                     new EqualsFilter('propertyGroupOptionId'$entity->getId())
  37.                 );
  38.                 /** @var PropertyGroupOptionIncrementEntity $increment */
  39.                 $increment $this->productGroupOptionIncrementRepository->search($criteriaContext::createDefaultContext())->first();
  40.                 if (!$increment) {
  41.                     $this->productGroupOptionIncrementRepository->upsert([['propertyGroupOptionId' => $entity->getId()]], Context::createDefaultContext());
  42.                     $increment $this->productGroupOptionIncrementRepository->search($criteriaContext::createDefaultContext())->first();
  43.                 }
  44.                 $struct = new AutoIncrementStruct($increment->getAutoIncrement());
  45.                 $entity->addExtension('autoIncrement'$struct);
  46.             }
  47.         } catch (\Throwable $exception) {
  48.             dd($exception);
  49.         }
  50.     }
  51. }