Completed
Push — master ( fd6c7c...1f9fde )
by Matze
09:32
created

CatchUserException::processException()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 37
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 4.0105

Importance

Changes 8
Bugs 1 Features 2
Metric Value
c 8
b 1
f 2
dl 0
loc 37
ccs 21
cts 23
cp 0.913
rs 8.5806
cc 4
eloc 26
nc 4
nop 2
crap 4.0105
1
<?php
2
3
namespace BrainExe\Core\Middleware;
4
5
use BrainExe\Core\Annotations\Middleware;
6
use BrainExe\Core\Application\UserException;
7
use BrainExe\Core\Traits\LoggerTrait;
8
use BrainExe\Core\Translation\TranslationTrait;
9
use Throwable;
10
use Symfony\Component\HttpFoundation\Request;
11
use Symfony\Component\HttpFoundation\Response;
12
use Symfony\Component\Routing\Exception\MethodNotAllowedException;
13
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
14
15
/**
16
 * @Middleware("Middleware.CatchUserException")
17
 */
18
class CatchUserException extends AbstractMiddleware
19
{
20
21
    use LoggerTrait;
22
    use TranslationTrait;
23
24
    /**
25
     * {@inheritdoc}
26
     */
27 7
    public function processException(Request $request, Throwable $exception)
28
    {
29 7
        if ($exception instanceof ResourceNotFoundException) {
30 2
            $exception = new UserException(
31 2
                $this->translate(
32 2
                    'Page not found: %s',
33 2
                    htmlspecialchars($request->getRequestUri())
34
                ),
35 2
                0,
36
                $exception
37
            );
38 2
            $response  = new Response('', 404);
39
        } elseif ($exception instanceof MethodNotAllowedException) {
40 1
            $exception = new UserException(
41 1
                $this->translate(
42 1
                    'You are not allowed to access the page. Allowed methods: %s',
43 1
                    implode(',', $exception->getAllowedMethods())
44
                ),
45 1
                0,
46
                $exception
47
            );
48 1
            $response = new Response('', 405);
49
        } elseif ($exception instanceof UserException) {
50
            // just pass a UserException to Frontend
51 1
            $response  = new Response('', 200);
52
        } else {
53 3
            $exception = new UserException($exception->getMessage(), 0, $exception);
54 3
            $response  = new Response('', 500);
55
        }
56
57 7
        $this->error($exception->getMessage());
58 7
        $this->error($exception->getTraceAsString());
59
60 7
        $this->setMessage($exception, $response);
61
62 7
        return $response;
63
    }
64
65
    /**
66
     * @param Throwable $exception
67
     * @param Response $response
68
     */
69 7
    protected function setMessage(Throwable $exception, Response $response)
70
    {
71 7
        $message = $exception->getMessage() ?: $this->translate('An error occurred');
72 7
        $response->headers->set('X-Flash-Type', 'danger');
73 7
        $response->headers->set('X-Flash-Message', $message);
74 7
        $response->setContent($message);
75 7
    }
76
}
77