CatchUserException::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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