summaryrefslogtreecommitdiff
path: root/src/Middleware/ErrorHandler.php
blob: 9fc347223593a2f4237eb3ff234e0baa7ea33f18 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php

namespace Engelsystem\Middleware;

use Engelsystem\Http\Exceptions\HttpException;
use Engelsystem\Http\Response;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Twig_LoaderInterface as TwigLoader;

class ErrorHandler implements MiddlewareInterface
{
    /** @var TwigLoader */
    protected $loader;

    /** @var string */
    protected $viewPrefix = 'errors/';

    /**
     * @param TwigLoader $loader
     */
    public function __construct(TwigLoader $loader)
    {
        $this->loader = $loader;
    }

    /**
     * Handles any error messages
     *
     * Should be added at the beginning
     *
     * @param ServerRequestInterface  $request
     * @param RequestHandlerInterface $handler
     * @return ResponseInterface
     */
    public function process(
        ServerRequestInterface $request,
        RequestHandlerInterface $handler
    ): ResponseInterface {
        try {
            $response = $handler->handle($request);
        } catch (HttpException $e) {
            $response = $this->createResponse($e->getMessage(), $e->getStatusCode(), $e->getHeaders());
        }

        $statusCode = $response->getStatusCode();
        $contentType = $response->getHeader('content-type');
        $contentType = array_shift($contentType);
        if (
            $statusCode < 400
            || !$response instanceof Response
            || !empty($contentType)
        ) {
            return $response;
        }

        $view = $this->selectView($statusCode);

        return $response->withView(
            $this->viewPrefix . $view,
            [
                'status'  => $statusCode,
                'content' => $response->getContent(),
            ],
            $statusCode,
            $response->getHeaders()
        );
    }

    /**
     * Select a view based on the given status code
     *
     * @param int $statusCode
     * @return string
     */
    protected function selectView(int $statusCode): string
    {
        $hundreds = intdiv($statusCode, 100);

        $viewsList = [$statusCode, $hundreds, $hundreds * 100];
        foreach ($viewsList as $view) {
            if ($this->loader->exists($this->viewPrefix . $view)) {
                return $view;
            }
        }

        return 'default';
    }

    /**
     * Create a new response
     *
     * @param string $content
     * @param int    $status
     * @param array  $headers
     * @return Response
     * @codeCoverageIgnore
     */
    protected function createResponse(string $content = '', int $status = 200, array $headers = [])
    {
        return response($content, $status, $headers);
    }
}