custom/plugins/X10SocialReviews/src/Subscribers/PageLoader.php line 48

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace X10\SocialReviews\Subscribers;
  3. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  4. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  5. use Shopware\Storefront\Page\GenericPageLoadedEvent;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Doctrine\DBAL\Connection;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  9. use X10\SocialReviews\Services\SocialReviewService;
  10. class PageLoader implements EventSubscriberInterface
  11. {
  12.     /**
  13.      * @var Connection
  14.      */
  15.     private $connection;
  16.     /**
  17.      * @var EntityRepositoryInterface $socialReviewRepository
  18.      */
  19.     private $socialReviewRepository;
  20.     /**
  21.      * @var SocialReviewService $socialReviewService
  22.      *
  23.      */
  24.     private $socialReviewService;
  25.     public function __construct(Connection $connection,
  26.                                 EntityRepositoryInterface $socialReviewRepository,
  27.                                 SocialReviewService $socialReviewService){
  28.         $this->connection $connection;
  29.         $this->socialReviewRepository $socialReviewRepository;
  30.         $this->socialReviewService $socialReviewService;
  31.     }
  32.     public static function getSubscribedEvents(): array
  33.     {
  34.         return [
  35.             GenericPageLoadedEvent::class => 'pageLoadedEvent',
  36.         ];
  37.     }
  38.     public function pageLoadedEvent(GenericPageLoadedEvent $event): void
  39.     {
  40.         /**
  41.          * wegen performance
  42.          */
  43.         $sql "SELECT CEIL(SUM(points) / count(*)) as summery FROM view_social_review";
  44.         $result $this->connection->executeQuery(
  45.             $sql
  46.         );
  47.         $summery $result->fetchAssociative()['summery'];
  48.         $table_name $this->socialReviewRepository->getDefinition()->getEntityName();
  49.         $sql "SELECT count(*) as total FROM $table_name";
  50.         $result $this->connection->executeQuery(
  51.             $sql
  52.         );
  53.         $total $result->fetchAssociative()['total'];
  54.         /*
  55.         $result = $this->socialReviewRepository->search((new Criteria())
  56.             ->setLimit(3)
  57.             ->addSorting(
  58.                 new FieldSorting('createdAt', FieldSorting::DESCENDING)
  59.             ), $event->getContext());*/
  60.         //$result = $this->socialReviewRepository->search((new Criteria()), $event->getContext());
  61.         /*
  62.         $total = $this->socialReviewRepository->search((new Criteria()), $event->getContext());
  63.         dump($total->count());exit;*/
  64.         $result $this->socialReviewService->getRawSqlReviewResults(30);
  65.         $event->getPage()->assign(['x10'=>
  66.             [
  67.                 'ratings' => $result,
  68.                 'ratingSummery'=>$summery,
  69.                 'ratingTotal'=>$total
  70.             ]
  71.         ]);
  72.         /**
  73.          * @var Facebook\Facebook
  74.         //$fb = $this->faceBookService->getFb();
  75.         try{
  76.             $decoded_body = $fb->get('/kartenmachen.de?fields=access_token')->getDecodedBody();
  77.             $fbRatings = $fb->get("/$decoded_body[id]/ratings?limit=5", $decoded_body['access_token']);
  78.             $event->getPage()->assign(['x10'=>['ratings' => $fbRatings->getDecodedBody()['data']]]);
  79.         }catch (\Facebook\Exceptions\FacebookResponseException $exception){
  80.             $event->getPage()->assign(['x10'=>['ratings' =>
  81.                 [],
  82.                 ]
  83.             ]);
  84.         }catch (\ErrorException $e){
  85.         }
  86.         */
  87.     }
  88. }