custom/static-plugins/SamsonCustomer/src/Subscriber/ReviewSubscriber.php line 43

Open in your IDE?
  1. <?php
  2. /**
  3.  * Date: 3/4/22
  4.  * Time: 1:53 PM
  5.  */
  6. namespace Samson\Subscriber;
  7. use Samson\Event\ReviewWrittenEvent;
  8. use Shopware\Core\Content\Product\ProductEvents;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  10. use Shopware\Core\Framework\Routing\Event\SalesChannelContextResolvedEvent;
  11. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  14. use Symfony\Component\Routing\RouterInterface;
  15. class ReviewSubscriber implements EventSubscriberInterface
  16. {
  17.     private EventDispatcherInterface $eventDispatcher;
  18.     private RouterInterface $router;
  19.     private SalesChannelContext $saleChannelContext;
  20.     public function __construct(EventDispatcherInterface $eventDispatcherRouterInterface $router)
  21.     {
  22.         $this->eventDispatcher $eventDispatcher;
  23.         $this->router $router;
  24.     }
  25.     public static function getSubscribedEvents(): array
  26.     {
  27.         return [
  28.             SalesChannelContextResolvedEvent::class => "onSalesChannelLoaded",
  29.             ProductEvents::PRODUCT_REVIEW_WRITTEN_EVENT => 'onReviewWritten'
  30.         ];
  31.     }
  32.     public function onSalesChannelLoaded(SalesChannelContextResolvedEvent $event){
  33.         $this->saleChannelContext $event->getSalesChannelContext();
  34.     }
  35.     public function onReviewWritten(EntityWrittenEvent $event): void
  36.     {
  37.         if ($this->saleChannelContext) {
  38.             $url $this->saleChannelContext->getSalesChannel()->getDomains()->first()->getUrl();
  39.             $url preg_replace('/\/$/'''$url);
  40.             $url $url $this->router->generate('administration.index') . '#/sw/review/detail/' $event->getWriteResults()[0]->getPrimaryKey();
  41.             $this->eventDispatcher->dispatch(
  42.                 new ReviewWrittenEvent(
  43.                     ReviewWrittenEvent::$eventName,
  44.                     $this->saleChannelContext->getSalesChannelId(),
  45.                     $event->getContext(),
  46.                     $url
  47.                 )
  48.             );
  49.         }
  50.     }
  51. }