Test Failed
Push — master ( 33fd98...a56c21 )
by Julien
07:13
created

DispatcherTrait::forward()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 2
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 3
rs 10
c 0
b 0
f 0
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 1
    public function callActionMethod($handler, string $actionMethod, array $params = [])
48
    {
49 1
        return call_user_func_array(
50 1
            [$handler, $actionMethod],
51 1
            array_filter($params, 'is_int', ARRAY_FILTER_USE_KEY)
52 1
        );
53
    }
54
    
55
    /**
56
     * Extending forwarding event to prevent cyclic routing when forwarding under dispatcher events
57
     * {@inheritDoc}
58
     */
59 1
    public function forward(array $forward, bool $preventCycle = false): void
60
    {
61 1
        $forward = $this->unsetForwardNullParts($forward);
62
        
63 1
        if (!$preventCycle) {
64 1
            parent::forward($forward);
65
        }
66
        
67 1
        elseif ($this->canForward($forward)) {
68 1
            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 2
    public function canForward(array $forward): bool
77
    {
78 2
        $parts = [
79 2
            'namespace' => $this->getNamespaceName(),
80 2
            'module' => $this->getModuleName(),
81 2
            'action' => $this->getActionName(),
82 2
            'params' => $this->getParams(),
83 2
        ];
84 2
        foreach ($parts as $part => $current) {
85 2
            if (isset($forward[$part]) && $current !== $forward[$part]) {
86 2
                return true;
87
            }
88
        }
89
        
90 2
        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 2
    private function canForwardHandler(array $forward): bool
100
    {
101 2
        if ($this->canForwardController($forward['controller'] ?? null)) {
102 1
            return true;
103
        }
104
        
105 2
        if ($this->canForwardTask($forward['task'] ?? null)) {
106 1
            return true;
107
        }
108
        
109 2
        return false;
110
    }
111
    
112
    /**
113
     * Check whether the controller is changed
114
     */
115 2
    private function canForwardController(?string $controller = null): bool
116
    {
117 2
        if ($this instanceof MvcDispatcher && isset($controller) && $this->getControllerName() !== $controller) {
118 1
            return true;
119
        }
120
        
121 2
        return false;
122
    }
123
    
124
    /**
125
     * Check whether the task is changed
126
     */
127 2
    private function canForwardTask(?string $task = null): bool
128
    {
129 2
        if ($this instanceof CliDispatcher && isset($task) && $this->getTaskName() !== $task) {
130 1
            return true;
131
        }
132
        
133 2
        return false;
134
    }
135
    
136 2
    public function unsetForwardNullParts(array $forward, ?array $parts = null): array
137
    {
138 2
        $parts ??= [
139 2
            'namespace',
140 2
            'module',
141 2
            'task',
142 2
            'controller',
143 2
            'action',
144 2
            'params',
145 2
        ];
146
        
147 2
        foreach ($parts as $part) {
148 2
            if (is_null($forward[$part] ?? null)) {
149 2
                unset($forward[$part]);
150
            }
151
        }
152
        
153 2
        return $forward;
154
    }
155
    
156 1
    public function toArray(): array
157
    {
158 1
        $ret = [
159 1
            'namespace' => $this->getNamespaceName(),
160 1
            'module' => $this->getModuleName(),
161 1
            'action' => $this->getActionName(),
162 1
            'params' => $this->getParams(),
163 1
            'handlerClass' => $this->getHandlerClass(),
164 1
            'handlerSuffix' => $this->getHandlerSuffix(),
165 1
            'activeMethod' => $this->getActiveMethod(),
166 1
            'actionSuffix' => $this->getActionSuffix(),
167 1
        ];
168
        
169 1
        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 1
        if ($this instanceof CliDispatcher) {
177
            $ret['task'] = $this->getTaskName();
178
            $ret['taskSuffix'] = $this->getTaskSuffix();
179
        }
180
        
181 1
        return $ret;
182
    }
183
}
184