src/Hitso/Bundle/CommonBundle/Controller/Controller.php line 171

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Hitso\Bundle\CommonBundle\Controller;
  4. use Doctrine\Common\Persistence\ObjectRepository;
  5. use Doctrine\Common\Util\ClassUtils;
  6. use FOS\UserBundle\Util\TokenGenerator;
  7. use Hitso\Bundle\BlocksBundle\Block\Factory;
  8. use Hitso\Bundle\CommonBundle\Doctrine\Factory\EntityCloneFactory;
  9. use Hitso\Bundle\CommonBundle\Doctrine\Manager\AbstractManager;
  10. use Hitso\Bundle\CommonBundle\Doctrine\Manager\ManagerCollection;
  11. use Hitso\Bundle\CommonBundle\Doctrine\Repository\EntityRepositoryInterface;
  12. use Hitso\Bundle\CommonBundle\Doctrine\Service\EntityAuthorizationChecker;
  13. use Hitso\Bundle\CommonBundle\Doctrine\Service\LoggableReverter;
  14. use Hitso\Bundle\CommonBundle\Doctrine\Service\LoggableReverterCollection;
  15. use Hitso\Bundle\CommonBundle\Doctrine\Service\LoggableReverterInterface;
  16. use Hitso\Bundle\CommonBundle\Entity\EntityInterface;
  17. use Hitso\Bundle\CommonBundle\Entity\User;
  18. use Hitso\Bundle\CommonBundle\Helper\Request\RequestHelperInterface;
  19. use Hitso\Bundle\CommonBundle\Helper\Router\RouterHelperInterface;
  20. use Hitso\Bundle\CommonBundle\Helper\Templating\TemplatingHelper;
  21. use Hitso\Bundle\CommonBundle\Helper\Templating\TemplatingHelperInterface;
  22. use Hitso\Bundle\CommonBundle\Media\UrlGenerator;
  23. use Hitso\Bundle\CommonBundle\UserLog\CrudActionDescriber;
  24. use Hitso\Bundle\ContentBundle\Entity\Content;
  25. use Hitso\Bundle\ContentBundle\Model\ContentTypeCollection;
  26. use Hitso\Bundle\ContentBundle\Services\ContentHelper;
  27. use Hitso\Bundle\MultiSiteBundle\MultiSite\Site;
  28. use Hitso\Bundle\MultiSiteBundle\MultiSite\SiteCollection;
  29. use Hitso\Bundle\MultiSiteBundle\MultiSite\SiteContext;
  30. use Hitso\Bundle\SectionBundle\Entity\Group;
  31. use Hitso\Bundle\SeoBundle\Service\SeoPresentation;
  32. use Sonata\SeoBundle\Seo\SeoPage;
  33. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  34. use Symfony\Component\HttpFoundation\RedirectResponse;
  35. use Symfony\Component\HttpFoundation\Response;
  36. use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
  37. use Symfony\Contracts\Translation\TranslatorInterface;
  38. use WhiteOctober\BreadcrumbsBundle\Model\Breadcrumbs;
  39. /**
  40.  * Class Controller
  41.  *
  42.  * @package Hitso\Bundle\CommonBundle\Controller
  43.  */
  44. abstract class Controller extends AbstractController
  45. {
  46.     public function getUser(): ?User
  47.     {
  48.         return parent::getUser();
  49.     }
  50.     /**
  51.      * @var SiteContext
  52.      */
  53.     private $siteContext;
  54.     public function initialize()
  55.     {
  56.     }
  57.     public function preExecute()
  58.     {
  59.     }
  60.     public function getTranslator(): TranslatorInterface
  61.     {
  62.         return $this->get('translator');
  63.     }
  64.     public function getSiteContext(): SiteContext
  65.     {
  66.         if (null === $this->siteContext) {
  67.             $this->siteContext $this->get('site.context');
  68.         }
  69.         return $this->siteContext;
  70.     }
  71.     public function getSiteCollection(): SiteCollection
  72.     {
  73.         return $this->get('site.collection');
  74.     }
  75.     public function getCurrentSite(): Site
  76.     {
  77.         $context $this->getSiteContext();
  78.         return $context->getContentSite() ?: $context->getRunningSite();
  79.     }
  80.     public function getRepository(string $class): ObjectRepository
  81.     {
  82.         $em         $this->getDoctrine()->getManager();
  83.         $repository $em->getRepository($class);
  84.         return $repository;
  85.     }
  86.     public function getBlock(string $namestring $siteId null): object
  87.     {
  88.         return $this->get(Factory::class)->getBlock($name$siteId);
  89.     }
  90.     public function describeLastCrudAction(EntityInterface $entitybool $flash true): ?string
  91.     {
  92.         $describer $this->get('crud_action_describer');
  93.         $action    $describer->describeLastActionText($entity);
  94.         if (null !== $action && true === $flash) {
  95.             $this->addFlash('success'$action);
  96.         }
  97.         return $action;
  98.     }
  99.     protected function updateSeoPageBySlug(string $slug): void
  100.     {
  101.         $page $this->getPageBySlug($slug);
  102.         if (null !== $page) {
  103.             $this->updateSeoPage($page);
  104.         }
  105.     }
  106.     public function updateSeoPage($content)
  107.     {
  108.         $this->getSeoPresentation()->updateSeoPage($content);
  109.     }
  110.     protected function getSeoPresentation(): SeoPresentation
  111.     {
  112.         return $this->get('cmf_seo.presentation');
  113.     }
  114.     protected function getSeoPage(): SeoPage
  115.     {
  116.         return $this->get('sonata.seo.page');
  117.     }
  118.     protected function generateContentUrl(Content $content$parameters = [], $referenceType UrlGeneratorInterface::ABSOLUTE_URL)
  119.     {
  120.         return $this->get(ContentHelper::class)->generateUrl($content$parameters$referenceType);
  121.     }
  122.     protected function redirectToAction(string $actionName 'index', array $params = []): RedirectResponse
  123.     {
  124.         $url $this->getRedirectToActionUrl($actionName$params);
  125.         return $this->redirectResponse($url);
  126.     }
  127.     protected function redirectResponse(string $urlint $status RedirectResponse::HTTP_FOUND): RedirectResponse
  128.     {
  129.         return new RedirectResponse($url$status);
  130.     }
  131.     protected function getRedirectToActionUrl(string $actionName 'index', array $params = []): string
  132.     {
  133.         return $this->getRouterHelper()->getRedirectToActionUrl($actionName$params);
  134.     }
  135.     protected function renderView(string $view, array $parameters = []): string
  136.     {
  137.         return $this->getTemplatingHelper()->render($view$parameters);
  138.     }
  139.     protected function displayTemplate(string $templateName, array $templateVars = []): Response
  140.     {
  141.         return $this->getTemplatingHelper()->renderControllerResponse($this$templateName$templateVars);
  142.     }
  143.     protected function getTemplatingHelper(): TemplatingHelperInterface
  144.     {
  145.         return $this->get('templating.helper');
  146.     }
  147.     protected function getRequestHelper(): RequestHelperInterface
  148.     {
  149.         return $this->get('request.helper');
  150.     }
  151.     protected function getRouterHelper(): RouterHelperInterface
  152.     {
  153.         return $this->get('router.helper');
  154.     }
  155.     protected function getPageBySlug(string $slug): ?Group
  156.     {
  157.         return $this->getRepository(Group::class)->findOneBy(['slug' => $slug]);
  158.     }
  159.     protected function getEntityManagerCollection()
  160.     {
  161.         return $this->get('hitso.entity_manager_collection');
  162.     }
  163.     /**
  164.      * @param EntityInterface $entity
  165.      *
  166.      * @return AbstractManager
  167.      */
  168.     protected function getManagerForEntity(EntityInterface $entity): AbstractManager
  169.     {
  170.         return $this->getManagerForClass(ClassUtils::getRealClass(get_class($entity)));
  171.     }
  172.     /**
  173.      * @param string $entityClass
  174.      *
  175.      * @return AbstractManager
  176.      */
  177.     protected function getManagerForClass(string $entityClass): AbstractManager
  178.     {
  179.         return $this->getEntityManagerCollection()->get($entityClass);
  180.     }
  181.     /**
  182.      * @param string $entityClass
  183.      *
  184.      * @return EntityRepositoryInterface
  185.      */
  186.     protected function getRepositoryForClass(string $entityClass): EntityRepositoryInterface
  187.     {
  188.         return $this->getEntityManagerCollection()->get($entityClass)->getRepository();
  189.     }
  190.     /**
  191.      * @return EntityCloneFactory
  192.      */
  193.     protected function getEntityCloneFactory(): EntityCloneFactory
  194.     {
  195.         return $this->get('hitso.entity_clone_factory');
  196.     }
  197.     /**
  198.      * @return LoggableReverter
  199.      */
  200.     protected function getLoggableReverter(): LoggableReverter
  201.     {
  202.         return $this->get('hitso.loggable_reverter');
  203.     }
  204.     protected function getEntityAuthorizationChecker(): EntityAuthorizationChecker
  205.     {
  206.         return $this->get('hitso.entity_authorization_checker');
  207.     }
  208.     protected function getLoggableReverterCollection(): LoggableReverterCollection
  209.     {
  210.         return $this->get('hitso.loggable_reverter_collection');
  211.     }
  212.     /**
  213.      * @param string $entityClass
  214.      *
  215.      * @return LoggableReverterInterface
  216.      */
  217.     protected function getReverterForClass(string $entityClass): LoggableReverterInterface
  218.     {
  219.         return $this->getLoggableReverterCollection()->get($entityClass);
  220.     }
  221.     /**
  222.      * @return array
  223.      */
  224.     public static function getSubscribedServices()
  225.     {
  226.         return array_merge(parent::getSubscribedServices(), [
  227.             'white_october_breadcrumbs'          => '?' Breadcrumbs::class,
  228.             'sonata.seo.page'                    => '?' SeoPage::class,
  229.             'site.context'                       => '?' SiteContext::class,
  230.             'site.collection'                    => '?' SiteCollection::class,
  231.             'cmf_seo.presentation'               => '?' SeoPresentation::class,
  232.             'templating.helper'                  => '?' TemplatingHelper::class,
  233.             'translator'                         => '?' TranslatorInterface::class,
  234.             'request.helper'                     => '?' RequestHelperInterface::class,
  235.             'router.helper'                      => '?' RouterHelperInterface::class,
  236.             'crud_action_describer'              => '?' CrudActionDescriber::class,
  237.             'hitso.content_types'                => '?' ContentTypeCollection::class,
  238.             'hitso.media_url_generator'          => '?' UrlGenerator::class,
  239.             'hitso.entity_manager_collection'    => '?' ManagerCollection::class,
  240.             'hitso.entity_clone_factory'         => '?' EntityCloneFactory::class,
  241.             'hitso.loggable_reverter'            => '?' LoggableReverter::class,
  242.             'hitso.entity_authorization_checker' => '?' EntityAuthorizationChecker::class,
  243.             'hitso.loggable_reverter_collection' => '?' LoggableReverterCollection::class,
  244.             'fos_user.util.token_generator'      => '?' TokenGenerator::class,
  245.         ]);
  246.     }
  247. }