custom/plugins/NewsletterSendinblue/src/Subscriber/CategorySubscriber.php line 151

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace NewsletterSendinblue\Subscriber;
  3. use Monolog\Logger;
  4. use NewsletterSendinblue\Service\BaseSyncService;
  5. use NewsletterSendinblue\Service\ConfigService;
  6. use NewsletterSendinblue\Traits\HelperTrait;
  7. use Shopware\Core\Content\Category\CategoryDefinition;
  8. use Shopware\Core\Content\Category\CategoryEntity;
  9. use Shopware\Core\Content\Category\CategoryEvents;
  10. use Shopware\Core\Defaults;
  11. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  12. use Shopware\Core\Framework\DataAbstractionLayer\EntityWriteResult;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityDeletedEvent;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\ChangeSetAware;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Write\Command\DeleteCommand;
  17. use Shopware\Core\Framework\DataAbstractionLayer\Write\Validation\PreWriteValidationEvent;
  18. use Shopware\Core\Framework\Uuid\Uuid;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. class CategorySubscriber implements EventSubscriberInterface
  21. {
  22.     use HelperTrait;
  23.     /** @var BaseSyncService */
  24.     private $categorySyncService;
  25.     /** @var EntityRepositoryInterface */
  26.     private $systemConfigRepository;
  27.     /** @var ConfigService */
  28.     private $configService;
  29.     /** @var Logger */
  30.     private $logger;
  31.     /** @var array */
  32.     private $deleteCategories = [];
  33.     /** @var array */
  34.     private $deletableIds = [];
  35.     /**
  36.      * @param BaseSyncService $categorySyncService
  37.      * @param EntityRepositoryInterface $systemConfigRepository
  38.      * @param ConfigService $configService
  39.      * @param Logger $logger
  40.      */
  41.     public function __construct(
  42.         BaseSyncService           $categorySyncService,
  43.         EntityRepositoryInterface $systemConfigRepository,
  44.         ConfigService             $configService,
  45.         Logger                    $logger
  46.     )
  47.     {
  48.         $this->categorySyncService $categorySyncService;
  49.         $this->systemConfigRepository $systemConfigRepository;
  50.         $this->configService $configService;
  51.         $this->logger $logger;
  52.     }
  53.     /**
  54.      * @return string[]
  55.      */
  56.     public static function getSubscribedEvents()
  57.     {
  58.         return [
  59.             CategoryEvents::CATEGORY_TRANSLATION_WRITTEN_EVENT => 'onCategoryWrittenEvent',
  60.             CategoryEvents::CATEGORY_DELETED_EVENT => 'onCategoryDeletedEvent',
  61.             PreWriteValidationEvent::class => 'onPreWriteValidationEvent',
  62.         ];
  63.     }
  64.     /**
  65.      * @param PreWriteValidationEvent $event
  66.      * @return void
  67.      */
  68.     public function onPreWriteValidationEvent(PreWriteValidationEvent $event): void
  69.     {
  70.         if ($event->getContext()->getVersionId() !== Defaults::LIVE_VERSION) {
  71.             return;
  72.         }
  73.         foreach ($event->getCommands() as $command) {
  74.             if (!$command instanceof ChangeSetAware) {
  75.                 continue;
  76.             }
  77.             if ($command->getDefinition()->getEntityName() === CategoryDefinition::ENTITY_NAME
  78.                 && get_class($command) === DeleteCommand::class
  79.                 && !empty($command->getPrimaryKey()['id'])
  80.             ) {
  81.                 $catId Uuid::fromBytesToHex($command->getPrimaryKey()['id']);
  82.                 $this->deletableIds[] = $catId;
  83.                 $childrenIds $this->categorySyncService->getChildrenIds($catId$event->getContext());
  84.                 $this->deletableIds array_merge($this->deletableIds$childrenIds);
  85.             }
  86.         }
  87.         foreach ($this->deletableIds as $id) {
  88.             $this->deleteCategories[$id] = $this->categorySyncService->getEntity($id$event->getContext());
  89.         }
  90.         if (!empty($this->deleteCategories)) {
  91.             $this->categorySyncService->setDeleteEntities($this->deleteCategories);
  92.         }
  93.     }
  94.     /**
  95.      * @param EntityDeletedEvent $event
  96.      * @return void
  97.      */
  98.     public function onCategoryDeletedEvent(EntityDeletedEvent $event): void
  99.     {
  100.         $connectionId $this->getAutoSyncConnectionId(
  101.             ConfigService::CONFIG_IS_CATEGORY_AUTO_SYNC_ENABLED,
  102.             $event->getContext()
  103.         );
  104.         if (empty($connectionId)) {
  105.             return;
  106.         }
  107.         foreach ($event->getWriteResults() as $writeResult) {
  108.             $categoryId $writeResult->getPrimaryKey();
  109.             if (empty($categoryId)) {
  110.                 continue;
  111.             }
  112.             if ($writeResult->getOperation() === EntityWriteResult::OPERATION_DELETE
  113.                 && isset($this->deleteCategories[$categoryId])
  114.                 && $this->deleteCategories[$categoryId] instanceof CategoryEntity
  115.             ) {
  116.                 foreach ($this->deleteCategories as $deleteCategory) {
  117.                     if ($deleteCategory instanceof CategoryEntity) {
  118.                         $this->categorySyncService->syncDelete($deleteCategory$connectionId$event->getContext());
  119.                     }
  120.                 }
  121.                 $this->deleteCategories = [];
  122.                 $this->categorySyncService->setDeleteEntities([]);
  123.                 break;
  124.             }
  125.         }
  126.     }
  127.     /**
  128.      * @param EntityWrittenEvent $event
  129.      * @return void
  130.      */
  131.     public function onCategoryWrittenEvent(EntityWrittenEvent $event): void
  132.     {
  133.         $connectionId $this->getAutoSyncConnectionId(
  134.             ConfigService::CONFIG_IS_CATEGORY_AUTO_SYNC_ENABLED,
  135.             $event->getContext()
  136.         );
  137.         if (empty($connectionId)) {
  138.             return;
  139.         }
  140.         foreach ($event->getWriteResults() as $writeResult) {
  141.             $categoryId $writeResult->getPrimaryKey()['categoryId'];
  142.             if (empty($categoryId)) {
  143.                 continue;
  144.             }
  145.             // it is for avoiding call "sync" action when custom field is saved
  146.             if (!$this->checkChangeSet($writeResult)) {
  147.                 continue;
  148.             }
  149.             if ($writeResult->getOperation() !== EntityWriteResult::OPERATION_DELETE) {
  150.                 /** @var CategoryEntity $category */
  151.                 $category $this->categorySyncService->getEntity($categoryId$event->getContext());
  152.                 if (!$category->getActive()) {
  153.                     $this->categorySyncService->syncDelete($category$connectionId$event->getContext(), true);
  154.                 } else {
  155.                     $this->categorySyncService->sync($categoryId$connectionId$event->getContext());
  156.                 }
  157.             }
  158.         }
  159.     }
  160. }