app/Plugin/SDream/EventSubscriber/Admin/ProductSyncSubscriber.php line 53

Open in your IDE?
  1. <?php
  2. /*
  3.     商品登録
  4.     
  5.     Plugin/SDream/
  6.     ├─ EventSubscriber/
  7.     │    └─ Admin
  8.     │       └─ ProductSyncSubscriber.php
  9.     │
  10.     ├─ Service/
  11.     │    └─ DisplayProductSyncService.php
  12.     │
  13.     └─ Resource/config/services.yaml
  14. */
  15. namespace Plugin\SDream\EventSubscriber\Admin;
  16. use Eccube\Event\EccubeEvents;
  17. use Eccube\Event\EventArgs;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. use Plugin\SDream\Service\DisplayProductSyncService;
  20. use Psr\Log\LoggerInterface;
  21. use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
  22. class ProductSyncSubscriber implements EventSubscriberInterface
  23. {
  24.     private $syncService;
  25.     private LoggerInterface $logger;
  26.     private FlashBagInterface $flashBag;
  27.     public function __construct(
  28.         DisplayProductSyncService $syncService,
  29.         LoggerInterface $logger,
  30.         FlashBagInterface $flashBag  
  31.         )
  32.     {
  33.         $this->syncService $syncService;
  34.         $this->logger $logger;
  35.         $this->flashBag $flashBag;
  36.     }
  37.     public static function getSubscribedEvents()
  38.     {
  39.         return [
  40.             EccubeEvents::ADMIN_PRODUCT_EDIT_COMPLETE => 'onProductEditComplete',
  41.         ];
  42.     }
  43.     public function onProductEditComplete(EventArgs $event)
  44.     {
  45.         $this->logger->info('onProductEditComplete called');
  46.         $Product $event->getArgument('Product');
  47.         // 管理画面に表示
  48.         $result $this->syncService->syncToDisplaySite($Product);
  49.         $session $event->getRequest()->getSession();
  50.         $session->set('display_sync_count'$result['updated']);
  51.         /*
  52.         if ($result['updated'] > 0) {
  53.             //$this->logger->info('onProductEditComplete updated');
  54.             $codeList = implode(', ', $result['codes']);
  55.             $event->getRequest()->getSession()->getFlashBag()
  56.                 ->add('admin',
  57.                     "表示サイトを {$result['updated']} 件更新しました。<br>更新コード: {$codeList}"
  58.                 );
  59.         } else {
  60.             $event->getRequest()->getSession()->getFlashBag()
  61.             ->add('error',
  62.                 "表示サイトの更新件数は0件です。商品コード構成が正しいか確認ください。なお価格が同額である場合は更新されません。"
  63.             );           
  64.         }
  65.             */
  66.     }
  67.     /*
  68.     イベントが拾えないので、KernelEvents::CONTROLLERを使う方法へ変更
  69.     ->>> ProductClassSyncSubscriber.php
  70.     EccubeEvents::ADMIN_PRODUCT_PRODUCT_CLASS_EDIT_UPDATE => 'onProductClassEditComplete',
  71.     public function onProductClassEditComplete(EventArgs $event)
  72.     {
  73.         $this->logger->info('onProductClassEditComplete called');
  74.         $ProductClass = $event->getArgument('ProductClass');
  75.         $Product = $ProductClass->getProduct();
  76.         $result = $this->syncService->syncToDisplaySite($Product);
  77.         if ($result['updated'] > 0) {
  78.             $event->getRequest()->getSession()->getFlashBag()
  79.                 ->add('admin', "表示サイトを {$result['updated']} 件更新しました。");
  80.         }
  81.     }
  82.       */
  83. }