Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
15 / 15 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
ControllerHelper | |
100.00% |
15 / 15 |
|
100.00% |
6 / 6 |
6 | |
100.00% |
1 / 1 |
__construct | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
render | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
redirectToRoute | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
addFlash | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
trans | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
createForm | |
100.00% |
1 / 1 |
|
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 | |
22 | declare(strict_types=1); |
23 | |
24 | namespace App\Application\Controller; |
25 | |
26 | use Symfony\Component\Form\FormFactoryInterface; |
27 | use Symfony\Component\Form\FormInterface; |
28 | use Symfony\Component\HttpFoundation\RedirectResponse; |
29 | use Symfony\Component\HttpFoundation\Response; |
30 | use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface; |
31 | use Symfony\Component\Routing\Generator\UrlGeneratorInterface; |
32 | use Symfony\Component\Routing\RouterInterface; |
33 | use Symfony\Contracts\Translation\TranslatorInterface; |
34 | use Twig\Environment; |
35 | |
36 | class ControllerHelper |
37 | { |
38 | /** |
39 | * @var Environment |
40 | */ |
41 | private $twig; |
42 | |
43 | /** |
44 | * @var FormFactoryInterface |
45 | */ |
46 | private $formFactory; |
47 | |
48 | /** |
49 | * @var RouterInterface |
50 | */ |
51 | private $router; |
52 | |
53 | /** |
54 | * @var FlashBagInterface |
55 | */ |
56 | private $flashBag; |
57 | |
58 | /** |
59 | * @var TranslatorInterface |
60 | */ |
61 | private $translator; |
62 | |
63 | public function __construct( |
64 | Environment $twig, |
65 | RouterInterface $router, |
66 | FormFactoryInterface $formFactory, |
67 | FlashBagInterface $flashBag, |
68 | TranslatorInterface $translator, |
69 | ) { |
70 | $this->twig = $twig; |
71 | $this->formFactory = $formFactory; |
72 | $this->router = $router; |
73 | $this->flashBag = $flashBag; |
74 | $this->translator = $translator; |
75 | } |
76 | |
77 | /** |
78 | * Render a Twig template as a response. |
79 | * |
80 | * @param string $name The name of the template |
81 | * @param array $context The context of the template |
82 | * |
83 | * @throws \Twig\Error\LoaderError |
84 | * @throws \Twig\Error\RuntimeError |
85 | * @throws \Twig\Error\SyntaxError |
86 | * |
87 | * @return Response The response with rendered twig template |
88 | */ |
89 | public function render(string $name, array $context = []): Response |
90 | { |
91 | $response = new Response(); |
92 | $response->setContent($this->twig->render($name, $context)); |
93 | |
94 | return $response; |
95 | } |
96 | |
97 | /** |
98 | * Redirect to provided route name |
99 | * - Look for URL of provided route name |
100 | * - Redirect to it. |
101 | * |
102 | * @param string $route The route name to use for redirect |
103 | * @param array $parameters The parameters to forward to redirect route |
104 | * @param int $status The status of the redirection |
105 | * |
106 | * @return RedirectResponse The redirect response |
107 | */ |
108 | public function redirectToRoute(string $route, array $parameters = [], int $status = 302): RedirectResponse |
109 | { |
110 | return new RedirectResponse( |
111 | $this->router->generate($route, $parameters, UrlGeneratorInterface::ABSOLUTE_PATH), |
112 | $status |
113 | ); |
114 | } |
115 | |
116 | /** |
117 | * Add Flashbag in session. |
118 | * |
119 | * @param string $type The flashbag type |
120 | * @param string $message The message of the flashbag |
121 | */ |
122 | public function addFlash(string $type, string $message): void |
123 | { |
124 | $this->flashBag->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, $data = null, array $options = []) |
151 | { |
152 | return $this->formFactory->create($type, $data, $options); |
153 | } |
154 | } |