custom/static-plugins/GlobusSW6/src/Subscriber/ProductReviewAndRatingSubscriber.php line 42

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace GlobusSW6\Subscriber;
  3. use GlobusSW6\Core\Content\Attributes\Product\Aggregate\ProductReview\ProductReviewAttributesEntity;
  4. use GlobusSW6\Core\Content\ProductRatings\VoteRatingsEntity;
  5. use GlobusSW6\Service\IaneoBusinessExtension;
  6. use GlobusSW6\Service\ProductReviewAndRatingService;
  7. use Shopware\Core\Content\Product\Aggregate\ProductReview\ProductReviewEntity;
  8. use Shopware\Core\Content\Product\ProductCollection;
  9. use Shopware\Core\Framework\Api\Context\AdminApiSource;
  10. use Shopware\Core\Framework\Api\Context\SystemSource;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntitySearchResultLoadedEvent;
  13. use Shopware\Core\Framework\Struct\ArrayStruct;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. class ProductReviewAndRatingSubscriber implements EventSubscriberInterface
  16. {
  17.     /** @var ProductReviewAndRatingService */
  18.     private $productReviewAndRatingService;
  19.     /** @var IaneoBusinessExtension */
  20.     private $ianeoBusinessExtensionService;
  21.     public function __construct(ProductReviewAndRatingService $productReviewAndRatingServiceIaneoBusinessExtension $ianeoBusinessExtensionService)
  22.     {
  23.         $this->productReviewAndRatingService $productReviewAndRatingService;
  24.         $this->ianeoBusinessExtensionService $ianeoBusinessExtensionService;
  25.     }
  26.     public static function getSubscribedEvents()
  27.     {
  28.         return [
  29.             'product.search.result.loaded' => 'onProductLoaded',
  30.             'product_review.loaded' => 'onReviewLoaded'
  31.         ];
  32.     }
  33.     public function onProductLoaded(EntitySearchResultLoadedEvent $event)
  34.     {
  35.         // do not fire event in backend context
  36.         if ($event->getContext()->getSource() instanceof AdminApiSource) {
  37.             if ($event->getContext()->getSource()->isAdmin()) {
  38.                 return;
  39.             }
  40.         }
  41.         if ($event->getContext()->getSource() instanceof SystemSource) {
  42.             return;
  43.         }
  44.         $salesChannelId $event->getContext()->getSource()->getSalesChannelId();
  45.         /** @var ProductCollection $products */
  46.         $products $event->getResult()->getEntities();
  47.         if ($products === null) {
  48.             return;
  49.         }
  50.         if ($event->getName() !== 'product.search.result.loaded') {
  51.             return;
  52.         }
  53.         $productReviewCount $this->productReviewAndRatingService->checkProductReviews($products$salesChannelId);
  54.         $averageRatings $this->productReviewAndRatingService->checkAverageRatings($products);
  55.         $productRecommandationCount $this->productReviewAndRatingService->checkProductRecommandations($products$salesChannelId);
  56.         $this->ianeoBusinessExtensionService->addAttributes($products$productReviewCount'productReviewCount');
  57.         $this->ianeoBusinessExtensionService->addAttributes($products$averageRatings'averageProductRating');
  58.         $this->ianeoBusinessExtensionService->addAttributes($products$productRecommandationCount'productRecommandationCount');
  59.     }
  60.     /**
  61.      * this method is used to append the customer name and up/down-votes to the ianeoAttributes so that
  62.      * this info is available in the routes
  63.      * (e.g. in  src/Core/Content/Product/SalesChannel/Review/ProductReviewRoute.php
  64.      *   /store-api/product/{productId}/reviews
  65.      * )
  66.      *
  67.      * @param EntityLoadedEvent $event
  68.      * @return void
  69.      */
  70.     public function onReviewLoaded(EntityLoadedEvent $event)
  71.     {
  72.         // do not fire event in backend context
  73.         if ($event->getContext()->getSource() instanceof AdminApiSource) {
  74.             if ($event->getContext()->getSource()->isAdmin()) {
  75.                 return;
  76.             }
  77.         }
  78.         $eventEntity $event->getEntities();
  79.         /** @var ProductReviewEntity $entity */
  80.         foreach ($eventEntity as $entity) {
  81.             $userInfo = new ArrayStruct();
  82.             $userInfo->set('firstname'$entity->getExternalUser());
  83.             /** @var  VoteRatingsEntity $vote */
  84.             $vote $entity->getExtension('vote');
  85.             $voteInfo = new ArrayStruct();
  86.             if (is_null($vote)) {
  87.                 $voteInfo->set('vote_info''no up- and downvotes available');
  88.             } else {
  89.                 $voteInfo->set('vote_id'$vote->getVoteId());
  90.                 $voteInfo->set('upvotes'$vote->getUpvotes());
  91.                 $voteInfo->set('downvotes'$vote->getDownvotes());
  92.             }
  93.             /** @var ProductReviewAttributesEntity $extension */
  94.             $extension $entity->getExtension('ianeoAttributes');
  95.             if (is_null($extension))
  96.             {
  97.                 //Todo SMN Errorhandling
  98.                 return;
  99.             }
  100.             $extension->addExtension('author_info'$userInfo);
  101.             $extension $entity->getExtension('ianeoAttributes');
  102.             if (is_null($extension))
  103.             {
  104.                 //Todo SMN Errorhandling
  105.                 return;
  106.             }
  107.             $extension->addExtension('vote_info'$voteInfo);
  108.         }
  109.     }
  110. }