<?php
namespace App\Controller;
use App\Entity\BlogTag;
use App\Helper\LastModifyHelper;
use App\Repository\BlogPostRepository;
use App\Repository\ReviewRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\KernelInterface;
use Symfony\Component\Routing\Annotation\Route;
class BusinessController extends AbstractController
{
private const RESTAURANTS_TAGS = ['кейсы', 'horeca'];
private const RESTAURANTS_RETAIL = ['кейсы', 'ритейл'];
private LastModifyHelper $tracker;
public function __construct(KernelInterface $appKernel)
{
$this->tracker = new LastModifyHelper($appKernel);
}
/**
* @Route("/business/restaurants", name="business.restaurants.ru", methods={"GET"}, requirements={"_locale":"ru"})
*/
public function restaurants(
ReviewRepository $reviewRepository,
BlogPostRepository $blogPostRepository
): Response
{
$reviews = $reviewRepository->findAllOrdered();
$query = $blogPostRepository->publishedAndTagSelective(self::RESTAURANTS_TAGS, 10000);
$posts = $query->getQuery()->getResult();
$countResult = 3;
foreach($posts as $id => $post) {
if (!$countResult) {
unset($posts[$id]);
continue;
}
$tg = [];
/** @var BlogTag $tag */
foreach($post->getTags() as $tag) {
$tg[] = $tag->getName();
}
if (count($tg) !== 2 ||
!in_array(current($tg), self::RESTAURANTS_TAGS) ||
!in_array(end($tg), self::RESTAURANTS_TAGS)
) {
unset($posts[$id]);
} else {
$countResult--;
}
}
$template = 'business/restaurants/index.html.twig';
$modified = $this->tracker->get($template);
$response = $this->render($template, [
'reviews' => $reviews,
'posts' => $posts,
'modified' => $modified
]);
$response->headers->set('Last-Modified', $modified);
return $response;
}
/**
* @Route("/business/retail", name="business.retail.ru", methods={"GET"}, requirements={"_locale":"ru"})
*/
public function retail(
ReviewRepository $reviewRepository,
BlogPostRepository $blogPostRepository
): Response
{
$reviews = $reviewRepository->findAllOrdered();
$query = $blogPostRepository->publishedAndTagSelective( self::RESTAURANTS_RETAIL, 10000);
$posts = $query->getQuery()->getResult();
$countResult = 3;
foreach($posts as $id => $post) {
if (!$countResult) {
unset($posts[$id]);
continue;
}
$tg = [];
/** @var BlogTag $tag */
foreach($post->getTags() as $tag) {
$tg[] = $tag->getName();
}
if (count($tg) !== 2 ||
!in_array(current($tg), self::RESTAURANTS_RETAIL) ||
!in_array(end($tg), self::RESTAURANTS_RETAIL)
) {
unset($posts[$id]);
} else {
$countResult--;
}
}
$template = 'business/retail/index.html.twig';
$modified = $this->tracker->get($template);
$response = $this->render($template, [
'reviews' => $reviews,
'posts' => $posts,
'modified' => $modified
]);
$response->headers->set('Last-Modified', $modified);
return $response;
}
}