ErrorController   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 4
dl 0
loc 61
ccs 0
cts 20
cp 0
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A notFound() 0 4 1
A error() 0 4 1
A render() 0 8 1
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
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...
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
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...
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