Test Failed
Push — master ( 702013...3aff2c )
by Julien
04:17
created

DispatcherTrait::canForwardHandler()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 3
Bugs 2 Features 0
Metric Value
cc 3
eloc 5
c 3
b 2
f 0
nc 3
nop 1
dl 0
loc 11
ccs 0
cts 0
cp 0
crap 12
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\Dispatcher;
13
14
use Phalcon\Mvc\Dispatcher as MvcDispatcher;
15
use Phalcon\Cli\Dispatcher as CliDispatcher;
16
17
trait DispatcherTrait
18
{
19
    abstract public function getNamespaceName(): string;
20
    
21
    abstract public function getModuleName(): string;
22
    
23
    abstract public function getActionName(): string;
24
    
25
    abstract public function getParams(): array;
26
    
27
    abstract public function getHandlerClass(): string;
28
    
29
    abstract public function getHandlerSuffix(): string;
30
31
//    abstract public function getTaskName(): string;
32
33
//    abstract public function getControllerName(): string;
34
    
35
    abstract public function getActionSuffix(): string;
36
    
37
    abstract public function getActiveMethod(): string;
38
    
39
    /**
40
     * {@inheritDoc}
41
     * The string typed keys are not passed to the action method arguments
42
     * Only the int keys will be passed
43
     *
44
     * @param $handler
45
     * @return mixed
46
     */
47
    public function callActionMethod($handler, string $actionMethod, array $params = [])
48
    {
49
        return call_user_func_array(
50
            [$handler, $actionMethod],
51
            array_filter($params, 'is_int', ARRAY_FILTER_USE_KEY)
52
        );
53
    }
54
    
55
    /**
56
     * Extending forwarding event to prevent cyclic routing when forwarding under dispatcher events
57
     * {@inheritDoc}
58
     */
59
    public function forward(array $forward, bool $preventCycle = false): void
60
    {
61
        $forward = $this->unsetForwardNullParts($forward);
62
        
63
        if (!$preventCycle) {
64
            parent::forward($forward);
65
        }
66
        
67
        elseif ($this->canForward($forward)) {
68
            parent::forward($forward);
69
        }
70
    }
71
    
72
    /**
73
     * Check whether the forward attribute can be forwarded
74
     * we do additional checks to prevent dispatcher cycling
75
     */
76
    public function canForward(array $forward): bool
77
    {
78
        $parts = [
79
            'namespace' => $this->getNamespaceName(),
80
            'module' => $this->getModuleName(),
81
            'action' => $this->getActionName(),
82
            'params' => $this->getParams(),
83
        ];
84
        foreach ($parts as $part => $current) {
85
            if (isset($forward[$part]) && $current !== $forward[$part]) {
86
                return true;
87
            }
88
        }
89
        
90
        return $this->canForwardHandler($forward);
91
    }
92
    
93
    /**
94
     * Check whether the handler is changed or not
95
     * depending on the dispatcher
96
     * MVC: controller
97
     * CLI: task
98
     */
99
    private function canForwardHandler(array $forward): bool
100
    {
101
        if ($this->canForwardController($forward['controller'] ?? null)) {
102
            return true;
103
        }
104
        
105
        if ($this->canForwardTask($forward['task'] ?? null)) {
106
            return true;
107
        }
108
        
109
        return false;
110
    }
111
    
112
    /**
113
     * Check whether the controller is changed
114
     */
115
    private function canForwardController(?string $controller = null): bool
116
    {
117
        if ($this instanceof MvcDispatcher && isset($controller) && $this->getControllerName() !== $controller) {
118
            return false;
119
        }
120
        
121
        return true;
122
    }
123
    
124
    /**
125
     * Check whether the task is changed
126
     */
127
    private function canForwardTask(?string $task = null): bool
128
    {
129
        if ($this instanceof CliDispatcher && isset($task) && $this->getTaskName() !== $task) {
130
            return false;
131
        }
132
        
133
        return true;
134
    }
135
    
136
    public function unsetForwardNullParts(array $forward, ?array $parts = null): array
137
    {
138
        $parts ??= [
139
            'namespace',
140
            'module',
141
            'task',
142
            'controller',
143
            'action',
144
            'params',
145
        ];
146
        
147
        foreach ($parts as $part) {
148
            if (is_null($forward[$part])) {
149
                unset($forward[$part]);
150
            }
151
        }
152
        
153
        return $forward;
154
    }
155
    
156
    public function toArray(): array
157
    {
158
        $ret = [
159
            'namespace' => $this->getNamespaceName(),
160
            'module' => $this->getModuleName(),
161
            'action' => $this->getActionName(),
162
            'params' => $this->getParams(),
163
            'handlerClass' => $this->getHandlerClass(),
164
            'handlerSuffix' => $this->getHandlerSuffix(),
165
            'activeMethod' => $this->getActiveMethod(),
166
            'actionSuffix' => $this->getActionSuffix(),
167
        ];
168
        
169
        if ($this instanceof MvcDispatcher) {
170
            $ret['controller'] = $this->getControllerName();
171
            $ret['previousNamespace'] = $this->getPreviousNamespaceName();
172
            $ret['previousController'] = $this->getPreviousControllerName();
173
            $ret['previousAction'] = $this->getPreviousActionName();
174
        }
175
        
176
        if ($this instanceof CliDispatcher) {
177
            $ret['task'] = $this->getTaskName();
178
            $ret['taskSuffix'] = $this->getTaskSuffix();
179
        }
180
        
181
        return $ret;
182
    }
183
}
184