<?php
declare(strict_types=1);
namespace Hitso\Bundle\CommonBundle\Controller;
use Doctrine\Common\Persistence\ObjectRepository;
use Doctrine\Common\Util\ClassUtils;
use FOS\UserBundle\Util\TokenGenerator;
use Hitso\Bundle\BlocksBundle\Block\Factory;
use Hitso\Bundle\CommonBundle\Doctrine\Factory\EntityCloneFactory;
use Hitso\Bundle\CommonBundle\Doctrine\Manager\AbstractManager;
use Hitso\Bundle\CommonBundle\Doctrine\Manager\ManagerCollection;
use Hitso\Bundle\CommonBundle\Doctrine\Repository\EntityRepositoryInterface;
use Hitso\Bundle\CommonBundle\Doctrine\Service\EntityAuthorizationChecker;
use Hitso\Bundle\CommonBundle\Doctrine\Service\LoggableReverter;
use Hitso\Bundle\CommonBundle\Doctrine\Service\LoggableReverterCollection;
use Hitso\Bundle\CommonBundle\Doctrine\Service\LoggableReverterInterface;
use Hitso\Bundle\CommonBundle\Entity\EntityInterface;
use Hitso\Bundle\CommonBundle\Entity\User;
use Hitso\Bundle\CommonBundle\Helper\Request\RequestHelperInterface;
use Hitso\Bundle\CommonBundle\Helper\Router\RouterHelperInterface;
use Hitso\Bundle\CommonBundle\Helper\Templating\TemplatingHelper;
use Hitso\Bundle\CommonBundle\Helper\Templating\TemplatingHelperInterface;
use Hitso\Bundle\CommonBundle\Media\UrlGenerator;
use Hitso\Bundle\CommonBundle\UserLog\CrudActionDescriber;
use Hitso\Bundle\ContentBundle\Entity\Content;
use Hitso\Bundle\ContentBundle\Model\ContentTypeCollection;
use Hitso\Bundle\ContentBundle\Services\ContentHelper;
use Hitso\Bundle\MultiSiteBundle\MultiSite\Site;
use Hitso\Bundle\MultiSiteBundle\MultiSite\SiteCollection;
use Hitso\Bundle\MultiSiteBundle\MultiSite\SiteContext;
use Hitso\Bundle\SectionBundle\Entity\Group;
use Hitso\Bundle\SeoBundle\Service\SeoPresentation;
use Sonata\SeoBundle\Seo\SeoPage;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use WhiteOctober\BreadcrumbsBundle\Model\Breadcrumbs;
/**
* Class Controller
*
* @package Hitso\Bundle\CommonBundle\Controller
*/
abstract class Controller extends AbstractController
{
public function getUser(): ?User
{
return parent::getUser();
}
/**
* @var SiteContext
*/
private $siteContext;
public function initialize()
{
}
public function preExecute()
{
}
public function getTranslator(): TranslatorInterface
{
return $this->get('translator');
}
public function getSiteContext(): SiteContext
{
if (null === $this->siteContext) {
$this->siteContext = $this->get('site.context');
}
return $this->siteContext;
}
public function getSiteCollection(): SiteCollection
{
return $this->get('site.collection');
}
public function getCurrentSite(): Site
{
$context = $this->getSiteContext();
return $context->getContentSite() ?: $context->getRunningSite();
}
public function getRepository(string $class): ObjectRepository
{
$em = $this->getDoctrine()->getManager();
$repository = $em->getRepository($class);
return $repository;
}
public function getBlock(string $name, string $siteId = null): object
{
return $this->get(Factory::class)->getBlock($name, $siteId);
}
public function describeLastCrudAction(EntityInterface $entity, bool $flash = true): ?string
{
$describer = $this->get('crud_action_describer');
$action = $describer->describeLastActionText($entity);
if (null !== $action && true === $flash) {
$this->addFlash('success', $action);
}
return $action;
}
protected function updateSeoPageBySlug(string $slug): void
{
$page = $this->getPageBySlug($slug);
if (null !== $page) {
$this->updateSeoPage($page);
}
}
public function updateSeoPage($content)
{
$this->getSeoPresentation()->updateSeoPage($content);
}
protected function getSeoPresentation(): SeoPresentation
{
return $this->get('cmf_seo.presentation');
}
protected function getSeoPage(): SeoPage
{
return $this->get('sonata.seo.page');
}
protected function generateContentUrl(Content $content, $parameters = [], $referenceType = UrlGeneratorInterface::ABSOLUTE_URL)
{
return $this->get(ContentHelper::class)->generateUrl($content, $parameters, $referenceType);
}
protected function redirectToAction(string $actionName = 'index', array $params = []): RedirectResponse
{
$url = $this->getRedirectToActionUrl($actionName, $params);
return $this->redirectResponse($url);
}
protected function redirectResponse(string $url, int $status = RedirectResponse::HTTP_FOUND): RedirectResponse
{
return new RedirectResponse($url, $status);
}
protected function getRedirectToActionUrl(string $actionName = 'index', array $params = []): string
{
return $this->getRouterHelper()->getRedirectToActionUrl($actionName, $params);
}
protected function renderView(string $view, array $parameters = []): string
{
return $this->getTemplatingHelper()->render($view, $parameters);
}
protected function displayTemplate(string $templateName, array $templateVars = []): Response
{
return $this->getTemplatingHelper()->renderControllerResponse($this, $templateName, $templateVars);
}
protected function getTemplatingHelper(): TemplatingHelperInterface
{
return $this->get('templating.helper');
}
protected function getRequestHelper(): RequestHelperInterface
{
return $this->get('request.helper');
}
protected function getRouterHelper(): RouterHelperInterface
{
return $this->get('router.helper');
}
protected function getPageBySlug(string $slug): ?Group
{
return $this->getRepository(Group::class)->findOneBy(['slug' => $slug]);
}
protected function getEntityManagerCollection()
{
return $this->get('hitso.entity_manager_collection');
}
/**
* @param EntityInterface $entity
*
* @return AbstractManager
*/
protected function getManagerForEntity(EntityInterface $entity): AbstractManager
{
return $this->getManagerForClass(ClassUtils::getRealClass(get_class($entity)));
}
/**
* @param string $entityClass
*
* @return AbstractManager
*/
protected function getManagerForClass(string $entityClass): AbstractManager
{
return $this->getEntityManagerCollection()->get($entityClass);
}
/**
* @param string $entityClass
*
* @return EntityRepositoryInterface
*/
protected function getRepositoryForClass(string $entityClass): EntityRepositoryInterface
{
return $this->getEntityManagerCollection()->get($entityClass)->getRepository();
}
/**
* @return EntityCloneFactory
*/
protected function getEntityCloneFactory(): EntityCloneFactory
{
return $this->get('hitso.entity_clone_factory');
}
/**
* @return LoggableReverter
*/
protected function getLoggableReverter(): LoggableReverter
{
return $this->get('hitso.loggable_reverter');
}
protected function getEntityAuthorizationChecker(): EntityAuthorizationChecker
{
return $this->get('hitso.entity_authorization_checker');
}
protected function getLoggableReverterCollection(): LoggableReverterCollection
{
return $this->get('hitso.loggable_reverter_collection');
}
/**
* @param string $entityClass
*
* @return LoggableReverterInterface
*/
protected function getReverterForClass(string $entityClass): LoggableReverterInterface
{
return $this->getLoggableReverterCollection()->get($entityClass);
}
/**
* @return array
*/
public static function getSubscribedServices()
{
return array_merge(parent::getSubscribedServices(), [
'white_october_breadcrumbs' => '?' . Breadcrumbs::class,
'sonata.seo.page' => '?' . SeoPage::class,
'site.context' => '?' . SiteContext::class,
'site.collection' => '?' . SiteCollection::class,
'cmf_seo.presentation' => '?' . SeoPresentation::class,
'templating.helper' => '?' . TemplatingHelper::class,
'translator' => '?' . TranslatorInterface::class,
'request.helper' => '?' . RequestHelperInterface::class,
'router.helper' => '?' . RouterHelperInterface::class,
'crud_action_describer' => '?' . CrudActionDescriber::class,
'hitso.content_types' => '?' . ContentTypeCollection::class,
'hitso.media_url_generator' => '?' . UrlGenerator::class,
'hitso.entity_manager_collection' => '?' . ManagerCollection::class,
'hitso.entity_clone_factory' => '?' . EntityCloneFactory::class,
'hitso.loggable_reverter' => '?' . LoggableReverter::class,
'hitso.entity_authorization_checker' => '?' . EntityAuthorizationChecker::class,
'hitso.loggable_reverter_collection' => '?' . LoggableReverterCollection::class,
'fos_user.util.token_generator' => '?' . TokenGenerator::class,
]);
}
}