NotFoundHandler::beforeException()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 4
nop 3
1
<?php
2
3
namespace TechPivot\Phalcon\Enterprise\Dispatcher\Plugins;
4
5
use Exception;
6
use Phalcon\Events\Event;
7
use Phalcon\Mvc\User\Plugin;
8
use Phalcon\Mvc\Dispatcher;
9
use Phalcon\Mvc\Dispatcher\Exception as DispatcherException;
10
11
/**
12
 * NotFoundHandler.
13
 *
14
 * Automatically forwards the dispatcher to the specified error handler when the dispatched route
15
 * results in an invalid action or invalid handler.
16
 */
17
class NotFoundHandler extends Plugin
18
{
19
    /**
20
     * The dispatch data to forward an invalid handler.
21
     *
22
     * @var array|null
23
     */
24
    private $forwardHandlerNotFound;
25
26
    /**
27
     * The dispatch data to forward an invalid action.
28
     *
29
     * @var array|null
30
     */
31
    private $forwardActionNotFound;
32
33
    /**
34
     * The dispatch data to forward an uncaught exception. The dispatcher will have an additional
35
     * parameter "exception" that contains the handled dispatch <tt>\Exception</tt>.
36
     *
37
     * @var array|null
38
     */
39
    private $forwardUnhandledException;
40
41
    /**
42
     * NotFoundPlugin constructor.
43
     *
44
     * @param array|null $forwardHandlerNotFound     The dispatch data to forward an invalid handler.
45
     * @param array|null $forwardActionNotFound      The dispatch data to forward an invalid action.
46
     * @param array|null $forwardUnhandledException  The dispatch data to forward an uncaught exception. The dispatcher
47
     *                                               will have an additional parameter "exception" that contains the
48
     *                                               handled dispatch <tt>\Exception</tt>.
49
     */
50
    public function __construct(array $forwardHandlerNotFound = null, array $forwardActionNotFound = null,
51
        array $forwardUnhandledException = null)
52
    {
53
        $this->forwardHandlerNotFound = $forwardHandlerNotFound;
54
        $this->forwardActionNotFound = $forwardActionNotFound;
55
        $this->forwardUnhandledException = $forwardUnhandledException;
56
    }
57
58
    /**
59
     * Handled when the dispatcher throws an exception of any kind.
60
     *
61
     * @param \Phalcon\Events\Event   $event       The beforeDispatchLoop event.
62
     * @param \Phalcon\Mvc\Dispatcher $dispatcher  The application dispatcher instance.
63
     * @param \Exception              $exception   The exception being handled.
64
     *
65
     * @return void|false  Returns <tt>false</tt> if the exception is dispatched to a specific error handler; otherwise
66
     *                     returns <tt>null</tt>.
67
     */
68
    public function beforeException(Event $event, Dispatcher $dispatcher, Exception $exception)
69
    {
70
        if ($exception instanceof DispatcherException) {
71
            switch ($exception->getCode()) {
72
                case Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
73
                    return $this->forward($dispatcher, $exception, $this->forwardHandlerNotFound);
74
75
                case Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
76
                    return $this->forward($dispatcher, $exception, $this->forwardActionNotFound);
77
            }
78
        }
79
80
        return $this->forward($dispatcher, $exception, $this->forwardUnhandledException);
81
    }
82
83
    /**
84
     * Convenience helper method to ensure that only dispatched exceptions return <tt>false</tt>, which is
85
     * important as this stops the current dispatch loop.
86
     *
87
     * @param \Phalcon\Mvc\Dispatcher $dispatcher  The application dispatcher instance.
88
     * @param \Exception              $exception   The exception being handled.
89
     * @param array|null              $forward     Optional dispatch data.
90
     *
91
     * @return void|false  Returns <tt>false</tt> if the exception is dispatched to a specific error handler; otherwise
92
     *                     returns <tt>null</tt>.
93
     */
94
    private function forward(Dispatcher $dispatcher, Exception $exception, array $forward = null)
95
    {
96
        if ($forward !== null) {
97
            if (isset($forward['params']) === false) {
98
                $forward['params'] = [];
99
            }
100
            $forward['params'] = array_merge($forward['params'], [
101
                'exception' => $exception
102
            ]);
103
            $dispatcher->forward($forward);
104
105
            return false;
106
        }
107
    }
108
}
109