1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Nastoletni\Code\UserInterface\Web\Controller; |
6
|
|
|
|
7
|
|
|
use Nastoletni\Code\UserInterface\Controller\AbstractController; |
8
|
|
|
use Psr\Http\Message\ResponseInterface as Response; |
9
|
|
|
use Psr\Http\Message\ServerRequestInterface as Request; |
10
|
|
|
use Throwable; |
11
|
|
|
|
12
|
|
|
class ErrorController extends AbstractController |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Handling 404 Not found. |
16
|
|
|
* |
17
|
|
|
* @param Request $request |
18
|
|
|
* @param Response $response |
19
|
|
|
* |
20
|
|
|
* @return Response |
21
|
|
|
*/ |
22
|
|
|
public function notFound(Request $request, Response $response): Response |
|
|
|
|
23
|
|
|
{ |
24
|
|
|
return $this->render($response, 404); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Handling 500 Internal server error. |
29
|
|
|
* |
30
|
|
|
* @param Request $request |
31
|
|
|
* @param Response $response |
32
|
|
|
* @param Throwable $exception |
33
|
|
|
* |
34
|
|
|
* @return Response |
35
|
|
|
*/ |
36
|
|
|
public function error(Request $request, Response $response, Throwable $exception): Response |
|
|
|
|
37
|
|
|
{ |
38
|
|
|
return $this->render($response, 500); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Takes care of all common things to these error pages, such as image from xkcd. |
43
|
|
|
* |
44
|
|
|
* @param Response $response |
45
|
|
|
* @param int $error |
46
|
|
|
* @return Response |
47
|
|
|
*/ |
48
|
|
|
private function render(Response $response, int $error): Response |
49
|
|
|
{ |
50
|
|
|
// FIXME: Maybe split this to separate repository? |
51
|
|
|
// 1851th is the last xkcd's comic available at the time of writing this. |
52
|
|
|
$number = random_int(1, 1851); |
53
|
|
|
$xkcdResponse = file_get_contents(sprintf('http://xkcd.com/%d/info.0.json', $number)); |
54
|
|
|
$xkcdImage = json_decode($xkcdResponse, true); |
55
|
|
|
|
56
|
|
|
return $this->twig->render($response, 'error.twig', [ |
57
|
|
|
'error' => $error, |
58
|
|
|
'xkcdImage' => $xkcdImage |
59
|
|
|
]) |
60
|
|
|
->withStatus($error); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.