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

CatchUserException::setMessage()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 3
Bugs 1 Features 1
Metric Value
c 3
b 1
f 1
dl 0
loc 7
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
crap 2
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