<?php declare(strict_types=1);
namespace X10\SocialReviews\Subscribers;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
use Shopware\Storefront\Page\GenericPageLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Doctrine\DBAL\Connection;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
use X10\SocialReviews\Services\SocialReviewService;
class PageLoader implements EventSubscriberInterface
{
/**
* @var Connection
*/
private $connection;
/**
* @var EntityRepositoryInterface $socialReviewRepository
*/
private $socialReviewRepository;
/**
* @var SocialReviewService $socialReviewService
*
*/
private $socialReviewService;
public function __construct(Connection $connection,
EntityRepositoryInterface $socialReviewRepository,
SocialReviewService $socialReviewService){
$this->connection = $connection;
$this->socialReviewRepository = $socialReviewRepository;
$this->socialReviewService = $socialReviewService;
}
public static function getSubscribedEvents(): array
{
return [
GenericPageLoadedEvent::class => 'pageLoadedEvent',
];
}
public function pageLoadedEvent(GenericPageLoadedEvent $event): void
{
/**
* wegen performance
*/
$sql = "SELECT CEIL(SUM(points) / count(*)) as summery FROM view_social_review";
$result = $this->connection->executeQuery(
$sql
);
$summery = $result->fetchAssociative()['summery'];
$table_name = $this->socialReviewRepository->getDefinition()->getEntityName();
$sql = "SELECT count(*) as total FROM $table_name";
$result = $this->connection->executeQuery(
$sql
);
$total = $result->fetchAssociative()['total'];
/*
$result = $this->socialReviewRepository->search((new Criteria())
->setLimit(3)
->addSorting(
new FieldSorting('createdAt', FieldSorting::DESCENDING)
), $event->getContext());*/
//$result = $this->socialReviewRepository->search((new Criteria()), $event->getContext());
/*
$total = $this->socialReviewRepository->search((new Criteria()), $event->getContext());
dump($total->count());exit;*/
$result = $this->socialReviewService->getRawSqlReviewResults(3, 0);
$event->getPage()->assign(['x10'=>
[
'ratings' => $result,
'ratingSummery'=>$summery,
'ratingTotal'=>$total
]
]);
/**
* @var Facebook\Facebook
//$fb = $this->faceBookService->getFb();
try{
$decoded_body = $fb->get('/kartenmachen.de?fields=access_token')->getDecodedBody();
$fbRatings = $fb->get("/$decoded_body[id]/ratings?limit=5", $decoded_body['access_token']);
$event->getPage()->assign(['x10'=>['ratings' => $fbRatings->getDecodedBody()['data']]]);
}catch (\Facebook\Exceptions\FacebookResponseException $exception){
$event->getPage()->assign(['x10'=>['ratings' =>
[],
]
]);
}catch (\ErrorException $e){
}
*/
}
}