vendor/crehler/mojebambino-engine/src/Subscriber/PropertyGroupSubscriber.php line 32

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Crehler\MojeBambinoEngine\Subscriber;
  3. use Crehler\MojeBambinoEngine\Service\CrehlerContextService;
  4. use Shopware\Core\Content\Property\PropertyEvents;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. class PropertyGroupSubscriber implements EventSubscriberInterface
  11. {
  12.     private EntityRepository $propertyGroupIncrementRepository;
  13.     private CrehlerContextService $crehlerContextService;
  14.     public function __construct(EntityRepository $propertyGroupIncrementRepositoryCrehlerContextService $crehlerContextService)
  15.     {
  16.         $this->propertyGroupIncrementRepository $propertyGroupIncrementRepository;
  17.         $this->crehlerContextService $crehlerContextService;
  18.     }
  19.     public static function getSubscribedEvents(): array
  20.     {
  21.         return [
  22.             PropertyEvents::PROPERTY_GROUP_LOADED_EVENT => 'onPropertyGroupLoaded'
  23.         ];
  24.     }
  25.     public function onPropertyGroupLoaded(EntityLoadedEvent $event): void
  26.     {
  27.         try {
  28.             if ($this->crehlerContextService->get(CrehlerContextService::PRODUCT_EXPORT_GENERATEfalse)) return;
  29.             $context $event->getContext();
  30.             foreach ($event->getEntities() as $entity) {
  31.                 if (null === $entity->getExtension('propertyGroupIncrement')) {
  32.                     $criteria = (new Criteria())
  33.                         ->addFilter(new EqualsFilter('propertyGroupId'$entity->getId()));
  34.                     $this->propertyGroupIncrementRepository->upsert([['propertyGroupId' => $entity->getId()]], $context);
  35.                     $increment $this->propertyGroupIncrementRepository->search($criteria$context)->first();
  36.                     $entity->addExtension('propertyGroupIncrement'$increment);
  37.                 }
  38.             }
  39.         } catch (\Throwable $exception) {
  40.             dd($exception);
  41.         }
  42.     }
  43. }