Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
ControllerHelper
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
6 / 6
6
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 render
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 redirectToRoute
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 addFlash
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 trans
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 createForm
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3/**
4 * This file is part of the MADIS - RGPD Management application.
5 *
6 * @copyright Copyright (c) 2018-2019 Soluris - Solutions Numériques Territoriales Innovantes
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22declare(strict_types=1);
23
24namespace App\Application\Controller;
25
26use Symfony\Component\Form\FormFactoryInterface;
27use Symfony\Component\Form\FormInterface;
28use Symfony\Component\HttpFoundation\RedirectResponse;
29use Symfony\Component\HttpFoundation\RequestStack;
30use Symfony\Component\HttpFoundation\Response;
31use Symfony\Component\HttpFoundation\Session\FlashBagAwareSessionInterface;
32use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
33use Symfony\Component\Routing\RouterInterface;
34use Symfony\Contracts\Translation\TranslatorInterface;
35use Twig\Environment;
36
37class ControllerHelper
38{
39    /**
40     * @var Environment
41     */
42    private $twig;
43
44    /**
45     * @var FormFactoryInterface
46     */
47    private $formFactory;
48
49    /**
50     * @var RouterInterface
51     */
52    private $router;
53
54    /**
55     * @var TranslatorInterface
56     */
57    private $translator;
58
59    private RequestStack $requestStack;
60
61    public function __construct(
62        Environment $twig,
63        RouterInterface $router,
64        FormFactoryInterface $formFactory,
65        TranslatorInterface $translator,
66        RequestStack $requestStack,
67    ) {
68        $this->twig         = $twig;
69        $this->formFactory  = $formFactory;
70        $this->router       = $router;
71        $this->translator   = $translator;
72        $this->requestStack = $requestStack;
73    }
74
75    /**
76     * Render a Twig template as a response.
77     *
78     * @param string $name    The name of the template
79     * @param array  $context The context of the template
80     *
81     * @throws \Twig\Error\LoaderError
82     * @throws \Twig\Error\RuntimeError
83     * @throws \Twig\Error\SyntaxError
84     *
85     * @return Response The response with rendered twig template
86     */
87    public function render(string $name, array $context = []): Response
88    {
89        $response = new Response();
90        $response->setContent($this->twig->render($name, $context));
91
92        return $response;
93    }
94
95    /**
96     * Redirect to provided route name
97     * - Look for URL of provided route name
98     * - Redirect to it.
99     *
100     * @param string $route      The route name to use for redirect
101     * @param array  $parameters The parameters to forward to redirect route
102     * @param int    $status     The status of the redirection
103     *
104     * @return RedirectResponse The redirect response
105     */
106    public function redirectToRoute(string $route, array $parameters = [], int $status = 302): RedirectResponse
107    {
108        return new RedirectResponse(
109            $this->router->generate($route, $parameters, UrlGeneratorInterface::ABSOLUTE_PATH),
110            $status
111        );
112    }
113
114    /**
115     * Add Flashbag in session.
116     *
117     * @param string $type    The flashbag type
118     * @param string $message The message of the flashbag
119     */
120    public function addFlash(string $type, string $message): void
121    {
122        /** @var FlashBagAwareSessionInterface $session */
123        $session = $this->requestStack->getSession();
124        $session->getFlashBag()->add($type, $message);
125    }
126
127    /**
128     * Translate a key.
129     *
130     * @param string  $key        The key to translate
131     * @param array   $parameters Parameters to use during translation
132     * @param ?string $domain     The domain on which look for translation
133     *
134     * @return string The translated key
135     */
136    public function trans(string $key, array $parameters = [], ?string $domain = null): string
137    {
138        return $this->translator->trans($key, $parameters, $domain);
139    }
140
141    /**
142     * Create a FormType.
143     *
144     * @param string      $type    The FormType name
145     * @param object|null $data    The data to use to create FormType
146     * @param array       $options The FormType options
147     *
148     * @return FormInterface The generated FormType
149     */
150    public function createForm(string $type, ?object $data = null, array $options = [])
151    {
152        return $this->formFactory->create($type, $data, $options);
153    }
154}