|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Nastoletni\Code\UserInterface\Web\Controller; |
|
6
|
|
|
|
|
7
|
|
|
use Nastoletni\Code\Domain\XkcdRepository; |
|
8
|
|
|
use Nastoletni\Code\UserInterface\Controller\AbstractController; |
|
9
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
|
10
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
|
11
|
|
|
use Throwable; |
|
12
|
|
|
|
|
13
|
|
|
class ErrorController extends AbstractController |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var XkcdRepository |
|
17
|
|
|
*/ |
|
18
|
|
|
private $xkcdRepository; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* ErrorController constructor. |
|
22
|
|
|
* |
|
23
|
|
|
* @param XkcdRepository $xkcdRepository |
|
24
|
|
|
*/ |
|
25
|
|
|
public function __construct(XkcdRepository $xkcdRepository) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->xkcdRepository = $xkcdRepository; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Handling 404 Not found. |
|
32
|
|
|
* |
|
33
|
|
|
* @param Request $request |
|
34
|
|
|
* @param Response $response |
|
35
|
|
|
* |
|
36
|
|
|
* @return Response |
|
37
|
|
|
*/ |
|
38
|
|
|
public function notFound(Request $request, Response $response): Response |
|
|
|
|
|
|
39
|
|
|
{ |
|
40
|
|
|
return $this->render($response, 404); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Handling 500 Internal server error. |
|
45
|
|
|
* |
|
46
|
|
|
* @param Request $request |
|
47
|
|
|
* @param Response $response |
|
48
|
|
|
* @param Throwable $exception |
|
49
|
|
|
* |
|
50
|
|
|
* @return Response |
|
51
|
|
|
*/ |
|
52
|
|
|
public function error(Request $request, Response $response, Throwable $exception): Response |
|
|
|
|
|
|
53
|
|
|
{ |
|
54
|
|
|
return $this->render($response, 500); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Takes care of all common things to these error pages, such as image from xkcd. |
|
59
|
|
|
* |
|
60
|
|
|
* @param Response $response |
|
61
|
|
|
* @param int $error |
|
62
|
|
|
* |
|
63
|
|
|
* @return Response |
|
64
|
|
|
*/ |
|
65
|
|
|
private function render(Response $response, int $error): Response |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->twig->render($response, 'error.twig', [ |
|
68
|
|
|
'error' => $error, |
|
69
|
|
|
'xkcdImage' => $this->xkcdRepository->getRandom(), |
|
70
|
|
|
]) |
|
71
|
|
|
->withStatus($error); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.