app/Plugin/ProductReview42/ProductReviewEvent.php line 69

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
  6.  *
  7.  * http://www.ec-cube.co.jp/
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Plugin\ProductReview42;
  13. use Eccube\Entity\Product;
  14. use Eccube\Event\TemplateEvent;
  15. use Eccube\Repository\Master\ProductStatusRepository;
  16. use Plugin\ProductReview42\Entity\ProductReviewStatus;
  17. use Plugin\ProductReview42\Repository\ProductReviewConfigRepository;
  18. use Plugin\ProductReview42\Repository\ProductReviewRepository;
  19. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  20. class ProductReviewEvent implements EventSubscriberInterface
  21. {
  22.     /**
  23.      * @var ProductReviewConfigRepository
  24.      */
  25.     protected $productReviewConfigRepository;
  26.     
  27.     /**
  28.      * @var ProductStatusRepository
  29.      */
  30.     protected $productStatusRepository;
  31.     /**
  32.      * @var ProductReviewRepository
  33.      */
  34.     protected $productReviewRepository;
  35.     /**
  36.      * ProductReview constructor.
  37.      *
  38.      * @param ProductReviewConfigRepository $productReviewConfigRepository
  39.      * @param ProductStatusRepository $productStatusRepository
  40.      * @param ProductReviewRepository $productReviewRepository
  41.      */
  42.     public function __construct(
  43.         ProductReviewConfigRepository $productReviewConfigRepository,
  44.         ProductStatusRepository $productStatusRepository,
  45.         ProductReviewRepository $productReviewRepository
  46.     ) {
  47.         $this->productReviewConfigRepository $productReviewConfigRepository;
  48.         $this->productStatusRepository $productStatusRepository;
  49.         $this->productReviewRepository $productReviewRepository;
  50.     }
  51.     /**
  52.      * @return array
  53.      */
  54.     public static function getSubscribedEvents()
  55.     {
  56.         return [
  57.             'Product/detail.twig' => 'detail',
  58.             'Block/review_block.twig' => 'onReviewBlock'
  59.         ];
  60.     }
  61.     public function onReviewBlock(TemplateEvent $event) {
  62.         $parameters $event->getParameters();
  63.         $ProductReviews $this->productReviewRepository->getTopReview();
  64.         $parameters['ProductReviews'] = $ProductReviews;
  65.         $event->setParameters($parameters);
  66.     }
  67.     /**
  68.      * @param TemplateEvent $event
  69.      */
  70.     public function detail(TemplateEvent $event)
  71.     {
  72.         // $event->addSnippet('ProductReview42/Resource/template/default/review.twig');
  73.         $Config $this->productReviewConfigRepository->get();
  74.         /** @var Product $Product */
  75.         $Product $event->getParameter('Product');
  76.         $ProductReviews $this->productReviewRepository->findBy(['Status' => ProductReviewStatus::SHOW'Product' => $Product], ['id' => 'DESC'], $Config->getReviewMax());
  77.         $rate $this->productReviewRepository->getAvgAll($Product);
  78.         $avg round($rate['recommend_avg']);
  79.         $count intval($rate['review_count']);
  80.         $parameters $event->getParameters();
  81.         $parameters['ProductReviews'] = $ProductReviews;
  82.         $parameters['ProductReviewAvg'] = $avg;
  83.         $parameters['ProductReviewCount'] = $count;
  84.         $parameters['ProductReviews'] = $ProductReviews;
  85.         $event->setParameters($parameters);
  86.     }
  87. }