Test Failed
Push — master ( 72bd91...62e8aa )
by Julien
04:22
created

Error::appendDefaultToRoute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 7
ccs 0
cts 7
cp 0
crap 2
rs 10
1
<?php
2
3
/**
4
 * This file is part of the Zemit Framework.
5
 *
6
 * (c) Zemit Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zemit\Mvc\Dispatcher;
13
14
use Exception;
15
use Phalcon\Dispatcher\Exception as DispatchException;
16
use Zemit\Di\Injectable;
17
use Phalcon\Events\Event;
18
use Zemit\Mvc\Dispatcher;
19
20
class Error extends Injectable
21
{
22
    public array $defaultNotFoundRoute = [
23
        'module' => null,
24
        'namespace' => null,
25
        'controller' => 'error',
26
        'action' => 'notFound',
27
    ];
28
    
29
    public array $defaultErrorRoute = [
30
        'module' => null,
31
        'namespace' => null,
32
        'controller' => 'error',
33
        'action' => 'fatal',
34
    ];
35
    
36
    /**
37
     * Forward to 404 or 500 error controller
38
     * @throws Exception
39
     */
40
    public function beforeException(Event $event, Dispatcher $dispatcher, Exception $exception): bool
1 ignored issue
show
Unused Code introduced by
The parameter $event is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

40
    public function beforeException(/** @scrutinizer ignore-unused */ Event $event, Dispatcher $dispatcher, Exception $exception): bool

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
41
    {
42
        switch ($exception->getCode()) {
43
            
44
            case DispatchException::EXCEPTION_HANDLER_NOT_FOUND:
45
            case DispatchException::EXCEPTION_ACTION_NOT_FOUND:
46
                if ($exception instanceof DispatchException) {
47
                    $route = $this->config->pathToArray('router.notFound') ?? [];
48
                    
49
                    $this->appendDefaultToRoute($route, $this->defaultNotFoundRoute);
50
                    $route['params']['exception'] = $exception;
51
                    
52
                    $dispatcher->forward($route, true);
53
                    return false;
54
                }
55
                break;
56
            
57
            default:
58
                http_response_code(500);
59
                
60
                // Everything else, if debug is false, forward to fatal error 500
61
                $appDebug = $this->config->path('app.debug', false);
62
                $debugEnable = $this->config->path('debug.enable', false);
63
                
64
                if (!$appDebug && !$debugEnable) {
65
                    $route = $this->config->pathToArray('router.error') ?? [];
66
                    
67
                    $this->appendDefaultToRoute($route, $this->defaultErrorRoute);
68
                    $route['params']['exception'] = $exception;
69
                    
70
                    $dispatcher->forward($route, true);
71
                    return false;
72
                }
73
                break;
74
        }
75
        throw $exception;
76
    }
77
    
78
    public function appendDefaultToRoute(array $route, array $default): array
79
    {
80
        $route['module'] ??= $default['module'] ?? null;
81
        $route['namespace'] ??= $default['namespace'] ?? null;
82
        $route['controller'] ??= $default['controller'] ?? null;
83
        $route['action'] ??= $default['action'] ?? null;
84
        return $route;
85
    }
86
}
87