summaryrefslogtreecommitdiff
path: root/src/Middleware/ErrorHandler.php
blob: 46e6e5a8138c3068bda0e32d0240e3719ac17872 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php

namespace Engelsystem\Middleware;

use Engelsystem\Http\Exceptions\HttpException;
use Engelsystem\Http\Exceptions\ValidationException;
use Engelsystem\Http\Request;
use Engelsystem\Http\Response;
use Illuminate\Support\Arr;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Twig\Loader\LoaderInterface as TwigLoader;

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

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

    /**
     * A list of inputs that are not saved from form input
     *
     * @var array
     */
    protected $formIgnore = [
        'password',
        'password_confirmation',
        'password2',
        'new_password',
        'new_password2',
        'new_pw',
        'new_pw2',
        '_token',
    ];

    /**
     * @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());
        } catch (ValidationException $e) {
            $response = $this->createResponse('', 302, ['Location' => $this->getPreviousUrl($request)]);

            if ($request instanceof Request) {
                $session = $request->getSession();
                $errors = array_merge_recursive(
                    $session->get('errors', []),
                    ['validation' => $e->getValidator()->getErrors()]
                );
                $session->set('errors', $errors);

                $session->set('form-data', Arr::except($request->request->all(), $this->formIgnore));
            }
        }

        $statusCode = $response->getStatusCode();
        $contentType = $response->getHeader('content-type');
        $contentType = array_shift($contentType);
        if (!$contentType && strpos($response->getBody(), '<html') !== false) {
            $contentType = 'text/html';
        }

        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);
    }

    /**
     * @param ServerRequestInterface $request
     * @return string
     */
    protected function getPreviousUrl(ServerRequestInterface $request)
    {
        if ($header = $request->getHeader('referer')) {
            return array_pop($header);
        }

        return '/';
    }
}