Completed
Push — master ( 5a398f...789151 )
by Albert
02:38
created

ErrorController   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
c 1
b 0
f 0
lcom 1
cbo 3
dl 0
loc 51
ccs 0
cts 19
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A notFound() 0 4 1
A error() 0 4 1
A render() 0 14 1
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
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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
0 ignored issues
show
Unused Code introduced by
The parameter $request is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $exception is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
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