Test Failed
Push — master ( 751bda...4a3c53 )
by Julien
04:22
created

DispatcherTrait::canForwardHandler()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 11
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 5
eloc 5
nc 3
nop 1
dl 0
loc 11
ccs 0
cts 0
cp 0
crap 30
rs 9.6111
c 2
b 1
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
    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->unsetForward($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
    public function canForwardHandler(array $forward): bool
100
    {
101
        if ($this instanceof MvcDispatcher && $this->getControllerName() !== $forward['controller'] ?? null) {
102
            return true;
103
        }
104
        
105
        if ($this instanceof CliDispatcher && $this->getTaskName() !== $forward['task'] ?? null) {
106
            return true;
107
        }
108
        
109
        return false;
110
    }
111
    
112
    public function unsetForward(array $forward, ?array $parts = null): array
113
    {
114
        $parts ??= [
115
            'namespace',
116
            'module',
117
            'task',
118
            'controller',
119
            'action',
120
            'params',
121
        ];
122
        
123
        foreach ($parts as $part) {
124
            if (is_null($forward[$part])) {
125
                unset($forward[$part]);
126
            }
127
        }
128
        
129
        return $forward;
130
    }
131
    
132
    public function toArray(): array
133
    {
134
        $ret = [
135
            'namespace' => $this->getNamespaceName(),
136
            'module' => $this->getModuleName(),
137
            'action' => $this->getActionName(),
138
            'params' => $this->getParams(),
139
            'handlerClass' => $this->getHandlerClass(),
140
            'handlerSuffix' => $this->getHandlerSuffix(),
141
            'activeMethod' => $this->getActiveMethod(),
142
            'actionSuffix' => $this->getActionSuffix(),
143
        ];
144
        
145
        if ($this instanceof MvcDispatcher) {
146
            $ret['controller'] = $this->getControllerName();
147
            $ret['previousNamespace'] = $this->getPreviousNamespaceName();
148
            $ret['previousController'] = $this->getPreviousControllerName();
149
            $ret['previousAction'] = $this->getPreviousActionName();
150
        }
151
        
152
        if ($this instanceof CliDispatcher) {
153
            $ret['task'] = $this->getTaskName();
154
            $ret['taskSuffix'] = $this->getTaskSuffix();
155
        }
156
        
157
        return $ret;
158
    }
159
}
160