1 | <?php |
||
2 | |||
3 | namespace App\Application\Responder; |
||
4 | |||
5 | use App\Module\Security\Exception\SecurityException; |
||
6 | use App\Module\Validation\Exception\ValidationException; |
||
7 | use Psr\Http\Message\ResponseInterface; |
||
8 | use Slim\Views\PhpRenderer; |
||
9 | |||
10 | final readonly class TemplateRenderer |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
11 | { |
||
12 | 38 | public function __construct(private PhpRenderer $phpRenderer) |
|
13 | { |
||
14 | 38 | } |
|
15 | |||
16 | /** |
||
17 | * Render template. |
||
18 | * |
||
19 | * @param ResponseInterface $response The response |
||
20 | * @param string $template Template pathname relative to templates directory |
||
21 | * @param array $data Associative array of template variables |
||
22 | * |
||
23 | * @return ResponseInterface The response |
||
24 | */ |
||
25 | 28 | public function render(ResponseInterface $response, string $template, array $data = []): ResponseInterface |
|
26 | { |
||
27 | 28 | return $this->phpRenderer->render($response, $template, $data); |
|
28 | } |
||
29 | |||
30 | /** |
||
31 | * Add global variable accessible in templates. |
||
32 | * |
||
33 | * @param string $key |
||
34 | * @param mixed $value |
||
35 | * |
||
36 | * @return void |
||
37 | */ |
||
38 | 10 | public function addPhpViewAttribute(string $key, mixed $value): void |
|
39 | { |
||
40 | 10 | $this->phpRenderer->addAttribute($key, $value); |
|
41 | } |
||
42 | |||
43 | /** |
||
44 | * Render template with validation errors. |
||
45 | * |
||
46 | * @param ResponseInterface $response |
||
47 | * @param string $template |
||
48 | * @param ValidationException $validationException |
||
49 | * @param array $queryParams same query params passed to page to be added again to form after validation error |
||
50 | * @param array|null $preloadValues |
||
51 | * |
||
52 | * @return ResponseInterface |
||
53 | */ |
||
54 | 5 | public function renderOnValidationError( |
|
55 | ResponseInterface $response, |
||
56 | string $template, |
||
57 | ValidationException $validationException, |
||
58 | array $queryParams = [], |
||
59 | ?array $preloadValues = null, |
||
60 | ): ResponseInterface { |
||
61 | 5 | $this->phpRenderer->addAttribute('preloadValues', $preloadValues); |
|
62 | |||
63 | // Add the validation errors to phpRender attributes |
||
64 | 5 | $this->phpRenderer->addAttribute('validation', $validationException->validationErrors); |
|
65 | 5 | $this->phpRenderer->addAttribute('formError', true); |
|
66 | // Provide same query params passed to page to be added again after validation error (e.g. redirect) |
||
67 | 5 | $this->phpRenderer->addAttribute('queryParams', $queryParams); |
|
68 | |||
69 | // Render template with status code |
||
70 | 5 | return $this->render($response->withStatus(422), $template); |
|
71 | } |
||
72 | |||
73 | /** |
||
74 | * Respond with delay user has to wait or action that needs to be made before repeating the action. |
||
75 | * Specifically for form errors. |
||
76 | * |
||
77 | * @param ResponseInterface $response |
||
78 | * @param string $template |
||
79 | * @param SecurityException $securityException |
||
80 | * @param array|null $preloadValues |
||
81 | * @param array $queryParams same query params passed to page to be added again to form after validation error |
||
82 | * |
||
83 | * @throws \Throwable |
||
84 | * |
||
85 | * @return ResponseInterface |
||
86 | */ |
||
87 | 3 | public function respondWithFormThrottle( |
|
88 | ResponseInterface $response, |
||
89 | string $template, |
||
90 | SecurityException $securityException, |
||
91 | array $queryParams = [], |
||
92 | ?array $preloadValues = null, |
||
93 | ): ResponseInterface { |
||
94 | 3 | $this->phpRenderer->addAttribute('throttleDelay', $securityException->getRemainingDelay()); |
|
95 | 3 | $this->phpRenderer->addAttribute('formErrorMessage', $securityException->getPublicMessage()); |
|
96 | 3 | $this->phpRenderer->addAttribute('preloadValues', $preloadValues); |
|
97 | 3 | $this->phpRenderer->addAttribute('formError', true); |
|
98 | |||
99 | // Provide same query params passed to page to be added again after validation error (e.g. redirect) |
||
100 | 3 | $this->phpRenderer->addAttribute('queryParams', $queryParams); |
|
101 | |||
102 | 3 | return $this->render($response->withStatus(422), $template); |
|
103 | } |
||
104 | } |
||
105 |