vendor/shopware/core/Checkout/Customer/Subscriber/ProductReviewSubscriber.php line 34

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Checkout\Customer\Subscriber;
  3. use Shopware\Core\Checkout\Customer\Service\ProductReviewCountService;
  4. use Shopware\Core\Content\Product\Aggregate\ProductReview\ProductReviewDefinition;
  5. use Shopware\Core\Defaults;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Event\BeforeDeleteEvent;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  8. use Shopware\Core\Framework\Log\Package;
  9. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  10. /**
  11.  * @internal
  12.  */
  13. #[Package('business-ops')]
  14. class ProductReviewSubscriber implements EventSubscriberInterface
  15. {
  16.     /**
  17.      * @internal
  18.      */
  19.     public function __construct(private readonly ProductReviewCountService $productReviewCountService)
  20.     {
  21.     }
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [
  25.             'product_review.written' => 'createReview',
  26.             BeforeDeleteEvent::class => 'deleteReview',
  27.         ];
  28.     }
  29.     public function deleteReview(BeforeDeleteEvent $event): void
  30.     {
  31.         $ids $event->getIds(ProductReviewDefinition::ENTITY_NAME);
  32.         if (empty($ids)) {
  33.             return;
  34.         }
  35.         $this->productReviewCountService->updateReviewCount($idstrue);
  36.     }
  37.     public function createReview(EntityWrittenEvent $reviewEvent): void
  38.     {
  39.         if (
  40.             $reviewEvent->getEntityName() !== ProductReviewDefinition::ENTITY_NAME
  41.             || $reviewEvent->getContext()->getVersionId() !== Defaults::LIVE_VERSION
  42.         ) {
  43.             return;
  44.         }
  45.         $this->productReviewCountService->updateReviewCount($reviewEvent->getIds());
  46.     }
  47. }