Test Failed
Push — master ( d1bf33...751bda )
by Julien
04:29
created

DispatcherTrait::canForwardHandler()   B

Complexity

Conditions 7
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 56

Importance

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