core/lib/Thelia/Controller/Front/BaseFrontController.php line 129

  1. <?php
  2. /*
  3.  * This file is part of the Thelia package.
  4.  * http://www.thelia.net
  5.  *
  6.  * (c) OpenStudio <info@thelia.net>
  7.  *
  8.  * For the full copyright and license information, please view the LICENSE
  9.  * file that was distributed with this source code.
  10.  */
  11. namespace Thelia\Controller\Front;
  12. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  13. use Thelia\Controller\BaseController;
  14. use Thelia\Core\HttpFoundation\Response;
  15. use Thelia\Core\HttpKernel\Exception\RedirectException;
  16. use Thelia\Core\Template\ParserInterface;
  17. use Thelia\Core\Template\TemplateDefinition;
  18. use Thelia\Model\AddressQuery;
  19. use Thelia\Model\ModuleQuery;
  20. class BaseFrontController extends BaseController
  21. {
  22.     public const CONTROLLER_TYPE 'front';
  23.     protected $currentRouter 'router.front';
  24.     public function checkAuth(): void
  25.     {
  26.         if ($this->getSecurityContext()->hasCustomerUser() === false) {
  27.             throw new RedirectException($this->retrieveUrlFromRouteId('customer.login.process'));
  28.         }
  29.     }
  30.     public function getControllerType(): string
  31.     {
  32.         return self::CONTROLLER_TYPE;
  33.     }
  34.     protected function checkCartNotEmpty(EventDispatcherInterface $eventDispatcher): void
  35.     {
  36.         $cart $this->getSession()->getSessionCart($eventDispatcher);
  37.         if ($cart === null || $cart->countCartItems() == 0) {
  38.             throw new RedirectException($this->retrieveUrlFromRouteId('cart.view'));
  39.         }
  40.     }
  41.     protected function checkValidDelivery(): void
  42.     {
  43.         $order $this->getSession()->getOrder();
  44.         if (null === $order
  45.             ||
  46.             null === $order->getChoosenDeliveryAddress()
  47.             ||
  48.             null === $order->getDeliveryModuleId()
  49.             ||
  50.             null === AddressQuery::create()->findPk($order->getChoosenDeliveryAddress())
  51.             ||
  52.             null === ModuleQuery::create()->findPk($order->getDeliveryModuleId())) {
  53.             throw new RedirectException($this->retrieveUrlFromRouteId('order.delivery'));
  54.         }
  55.     }
  56.     protected function checkValidInvoice(): void
  57.     {
  58.         $order $this->getSession()->getOrder();
  59.         if (null === $order
  60.             ||
  61.             null === $order->getChoosenInvoiceAddress()
  62.             ||
  63.             null === $order->getPaymentModuleId()
  64.             ||
  65.             null === AddressQuery::create()->findPk($order->getChoosenInvoiceAddress())
  66.             ||
  67.             null === ModuleQuery::create()->findPk($order->getPaymentModuleId())) {
  68.             throw new RedirectException($this->retrieveUrlFromRouteId('order.invoice'));
  69.         }
  70.     }
  71.     /**
  72.      * @param TemplateDefinition $template the template to process, or null for using the front template
  73.      *
  74.      * @return ParserInterface the Thelia parser²
  75.      */
  76.     protected function getParser($template null)
  77.     {
  78.         $parser $this->container->get('thelia.parser');
  79.         // Define the template that should be used
  80.         $parser->setTemplateDefinition(
  81.             $template ?: $this->getTemplateHelper()->getActiveFrontTemplate(),
  82.             $this->useFallbackTemplate
  83.         );
  84.         return $parser;
  85.     }
  86.     /**
  87.      * Render the given template, and returns the result as an Http Response.
  88.      *
  89.      * @param string $templateName the complete template name, with extension
  90.      * @param array  $args         the template arguments
  91.      * @param int    $status       http code status
  92.      *
  93.      * @return \Thelia\Core\HttpFoundation\Response
  94.      */
  95.     protected function render($templateName$args = [], $status 200)
  96.     {
  97.         return new Response($this->renderRaw($templateName$args), $status);
  98.     }
  99.     /**
  100.      * Render the given template, and returns the result as a string.
  101.      *
  102.      * @param string $templateName the complete template name, with extension
  103.      * @param array  $args         the template arguments
  104.      * @param string$templateDir
  105.      *
  106.      * @return string
  107.      */
  108.     protected function renderRaw($templateName$args = [], $templateDir null)
  109.     {
  110.         // Add the template standard extension
  111.         $templateName .= '.html';
  112.         // Render the template.
  113.         $data $this->getParser($templateDir)->render($templateName$args);
  114.         return $data;
  115.     }
  116. }