src/UI/WebBundle/Controller/AbstractController.php line 202

Open in your IDE?
  1. <?php
  2. namespace Whater\UI\WebBundle\Controller;
  3. use Monolog\Logger;
  4. use Whater\Domain\User\Model\User;
  5. use Whater\Infrastructure\SecurityBundle\Security\Model\Auth;
  6. use JMS\Serializer\Serializer;
  7. use Cocur\Slugify\Slugify;
  8. use FOS\RestBundle\View\View;
  9. use FOS\RestBundle\View\ViewHandlerInterface;
  10. use Knp\Snappy\Pdf;
  11. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpFoundation\RequestStack;
  14. use Symfony\Component\Form\FormFactory;
  15. use Symfony\Component\HttpFoundation\Session\Session;
  16. use Symfony\Bundle\FrameworkBundle\Routing\Router;
  17. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  18. use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
  19. use FOS\RestBundle\Controller\ControllerTrait as FOSControllerTrait;
  20. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  21. use Symfony\Contracts\Translation\TranslatorInterface;
  22. use Twig\Environment;
  23. use GeoIp2\Database\Reader;
  24. /**
  25. * Class AbstractController
  26. *
  27. * @package Whater\UI\WebBundle\Controller
  28. */
  29. abstract class AbstractController extends \Symfony\Bundle\FrameworkBundle\Controller\AbstractController
  30. {
  31. use FOSControllerTrait;
  32. protected function getViewHandler(): ViewHandlerInterface
  33. {
  34. return $this->viewHandler();
  35. }
  36. protected function viewHandler(): ViewHandlerInterface
  37. {
  38. return $this->container->get('fos_rest.view_handler');
  39. }
  40. protected function requestStack(): RequestStack
  41. {
  42. return $this->container->get('request_stack');
  43. }
  44. protected function templating(): Environment
  45. {
  46. return $this->container->get('twig');
  47. }
  48. protected function formFactory(): FormFactory
  49. {
  50. return $this->container->get('form.factory');
  51. }
  52. protected function session(): Session
  53. {
  54. return $this->container->get('session');
  55. }
  56. protected function router(): Router
  57. {
  58. return $this->container->get('router');
  59. }
  60. protected function translator(): TranslatorInterface
  61. {
  62. return $this->container->get('translator');
  63. }
  64. protected function jmsSerializer(): Serializer
  65. {
  66. return $this->container->get('jms_serializer');
  67. }
  68. protected function authorizationChecker(): AuthorizationCheckerInterface
  69. {
  70. return $this->container->get('security.authorization_checker');
  71. }
  72. protected function tokenStorage(): TokenStorageInterface
  73. {
  74. return $this->container->get('security.token_storage');
  75. }
  76. protected function eventDispatcher(): EventDispatcherInterface
  77. {
  78. return $this->container->get('event_dispatcher');
  79. }
  80. protected function knpSnappyPdf(): Pdf
  81. {
  82. return $this->container->get('knp_snappy.pdf');
  83. }
  84. protected function logger(): Logger
  85. {
  86. return $this->container->get('whater.logger');
  87. }
  88. protected function slugify(): Slugify
  89. {
  90. return $this->container->get('slugify');
  91. }
  92. protected function geoIpReader(): Reader
  93. {
  94. return $this->container->get('geoip2.reader');
  95. }
  96. private $parameterBag;
  97. /**
  98. * @param ParameterBagInterface $parameterBag
  99. */
  100. public function setParameterBag(ParameterBagInterface $parameterBag)
  101. {
  102. $this->parameterBag = $parameterBag;
  103. }
  104. protected function parameterBag(): ParameterBagInterface
  105. {
  106. return $this->parameterBag;
  107. }
  108. protected $form;
  109. protected $form_handler;
  110. protected $form_errors = array();
  111. protected $errors = array();
  112. /**
  113. *
  114. * @param FormType $formType
  115. * @param string $formName
  116. * @param boolean $csrfProtection
  117. * @return Form
  118. */
  119. protected function getFormFactory()
  120. {
  121. return $this->formFactory();
  122. }
  123. /**
  124. *
  125. * @param type $action
  126. * @param type $value
  127. */
  128. protected function setFlash($action, $value)
  129. {
  130. $this->session()->getFlashBag()->add($action, $value);
  131. }
  132. /**
  133. *
  134. * @param type $action
  135. * @return type
  136. */
  137. protected function getFlash($action)
  138. {
  139. $values = $this->session()->getFlashBag()->get($action);
  140. if (empty($values)) {
  141. return null;
  142. } else if (\count($values) == 1) {
  143. return $values[0];
  144. } else {
  145. return $values;
  146. }
  147. }
  148. /**
  149. * Returns a rendered view.
  150. *
  151. * @param string $view The view name
  152. * @param array $parameters An array of parameters to pass to the view
  153. *
  154. * @return string The rendered view
  155. */
  156. protected function renderView(string $view, array $parameters = array()): string
  157. {
  158. return $this->templating()->render($view, $parameters);
  159. }
  160. /**
  161. * Renders a view.
  162. *
  163. * @param string $view The view name
  164. * @param array $parameters An array of parameters to pass to the view
  165. * @param Response $response A response instance
  166. *
  167. * @return Response A Response instance
  168. */
  169. protected function render(string $view, array $parameters = array(), Response $response = null): Response
  170. {
  171. if ($response == null) {
  172. return new Response($this->templating()->render($view, $parameters));
  173. } else {
  174. $response->setContent($this->templating()->render($view, $parameters));
  175. return $response;
  176. }
  177. }
  178. /**
  179. * Check if user has a role
  180. *
  181. * @return true|false
  182. */
  183. protected function isGranted($attributes, $subject = null): bool
  184. {
  185. return $this->authorizationChecker()->isGranted($attributes, $subject);
  186. }
  187. /**
  188. * Return the authenticated auth
  189. *
  190. * @return ?User
  191. */
  192. protected function getGrantedAuth(): ?Auth
  193. {
  194. if (is_null($this->tokenStorage())) {
  195. return null;
  196. }
  197. $token = $this->tokenStorage()->getToken();
  198. if (empty($token)) {
  199. return null;
  200. }
  201. $auth = $token->getUser();
  202. if (empty($auth) || is_string($auth)) {
  203. return null;
  204. }
  205. return $auth;
  206. }
  207. /**
  208. * Return the authenticated user
  209. *
  210. * @return ?User
  211. */
  212. protected function getGrantedUser(): ?User
  213. {
  214. $auth = $this->getGrantedAuth();
  215. if (empty($auth)) {
  216. return null;
  217. }
  218. return $auth->user();
  219. }
  220. /**
  221. * Bad Request response
  222. *
  223. * @param string $data
  224. *
  225. * @return View
  226. */
  227. protected function sendBadRequestResponse($data = null)
  228. {
  229. $view = View::create();
  230. $view->setStatusCode(400);
  231. $view->setData(array('message' => $data));
  232. return $this->viewHandler()->handle($view);
  233. }
  234. /**
  235. * Unauthorized Request response
  236. *
  237. * @param string $data
  238. *
  239. * @return View
  240. */
  241. protected function sendUnauthorizedRequestResponse($data = null)
  242. {
  243. $view = View::create();
  244. $view->setStatusCode(401);
  245. $view->setData(array('message' => $data));
  246. return $this->viewHandler()->handle($view);
  247. }
  248. }