custom/plugins/WabsMaterialstammSync/src/Subscriber/WabsMaterialstammSyncSubscriber.php line 65

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace WabsMaterialstammSync\Subscriber;
  3. use Exception;
  4. use Psr\Log\LoggerInterface;
  5. use Shopware\Core\Content\Category\CategoryEvents;
  6. use Shopware\Core\Content\Product\ProductEvents;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use WabsMaterialstammSync\Exceptions\WabsMaterialstammSyncException;
  10. use WabsMaterialstammSync\Service\WabsMaterialstammSubsriberService;
  11. class WabsMaterialstammSyncSubscriber implements EventSubscriberInterface
  12. {
  13.     /** @var WabsMaterialstammSubsriberService $WabsMaterialstammSubsriberService */
  14.     private $wabsMaterialstammSubsriberService;
  15.     /** @var LoggerInterface $logger */
  16.     private $logger;
  17.     public function __construct(
  18.         WabsMaterialstammSubsriberService $wabsMaterialstammSubsriberService,
  19.         LoggerInterface $logger
  20.     ) {
  21.         $this->wabsMaterialstammSubsriberService $wabsMaterialstammSubsriberService;
  22.         $this->logger $logger;
  23.     }
  24.     public static function getSubscribedEvents(): array
  25.     {
  26.         return [
  27.             ProductEvents::PRODUCT_WRITTEN_EVENT => "onProductWritten",
  28.             CategoryEvents::CATEGORY_WRITTEN_EVENT => "onCategoryWritten"
  29.         ];
  30.     }
  31.     /**
  32.      * @param EntityWrittenEvent $event
  33.      */
  34.     public function onProductWritten(EntityWrittenEvent $event)
  35.     {
  36.         //only execute if the context source is admin
  37.         if (!$this->isAdminContextSource($event)) {
  38.             return;
  39.         }
  40.         try {
  41.             $this->logger->debug("--- Starting onProductWritten Event - WabsMaterialstammSync\Subscriber");
  42.             $productIds = [];
  43.             foreach ($event->getWriteResults() as $writeResult) {
  44.                 $productIds[] = $writeResult->getPrimaryKey();
  45.             }
  46.             $this->wabsMaterialstammSubsriberService->updateMasterdataIsSync($productIdsfalse);
  47.             $this->logger->debug("--- onProductWritten found " count($productIds) . " ids");
  48.         } catch (Exception $exception) {
  49.             $this->logger->error($exception->getMessage());
  50.         }
  51.     }
  52.     /**
  53.      * @param EntityWrittenEvent $event
  54.      */
  55.     public function onCategoryWritten(EntityWrittenEvent $event)
  56.     {
  57.         //only execute if the context source is admin
  58.         if (!$this->isAdminContextSource($event)) {
  59.             return;
  60.         }
  61.         try {
  62.             $this->logger->debug("--- Starting onCategoryWritten Event - WabsMaterialstammSync\Subscriber");
  63.             $categoryIds = [];
  64.             foreach ($event->getWriteResults() as $writeResult) {
  65.                 $categoryIds[] = $writeResult->getPrimaryKey();
  66.             }
  67.             $categoryIds $this->wabsMaterialstammSubsriberService->getCategoriesIdsByIds($categoryIds);
  68.             $productIds $this->wabsMaterialstammSubsriberService->getProductIdsByCategoryIds($categoryIds);
  69.             if (empty($productIds)) {
  70.                 throw new WabsMaterialstammSyncException("No Products found");
  71.             }
  72.             $this->wabsMaterialstammSubsriberService->updateMasterdataIsSync($productIdsfalse);
  73.             $this->logger->debug("--- onCategoryWritten found " count($productIds), $productIds);
  74.         } catch (Exception $exception) {
  75.             $this->logger->error($exception->getMessage());
  76.         }
  77.     }
  78.     private function isAdminContextSource(EntityWrittenEvent $event): bool
  79.     {
  80.         return is_a($event->getContext()->getSource(), "Shopware\Core\Framework\Api\Context\AdminApiSource");
  81.     }
  82. }