Test Failed
Push — master ( 535fd6...a48418 )
by Julien
13:35
created

DispatcherTrait::callActionMethod()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 3
c 1
b 0
f 0
dl 0
loc 5
ccs 0
cts 4
cp 0
rs 10
cc 1
nc 1
nop 3
crap 2
1
<?php
2
/**
3
 * This file is part of the Zemit Framework.
4
 *
5
 * (c) Zemit Team <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE.txt
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Zemit\Dispatcher;
12
13
use Phalcon\Mvc\Dispatcher as MvcDispatcher;
14
use Phalcon\Cli\Dispatcher as CliDispatcher;
15
16
/**
17
 * Class DispatcherTrait
18
 *
19
 * @author Julien Turbide <[email protected]>
20
 * @copyright Zemit Team <[email protected]>
21
 *
22
 * @since 1.0
23
 * @version 1.0
24
 *
25
 * @package Zemit\Dispatcher
26
 */
27
trait DispatcherTrait
28
{
29
    /**
30
     * @param $handler
31
     * @param string $actionMethod
32
     * @param array $params
33
     * @return mixed
34
     */
35
    public function callActionMethod($handler, string $actionMethod, array $params = [])
36
    {
37
        return call_user_func_array(
38
            [$handler, $actionMethod],
39
            array_filter($params, 'is_int', ARRAY_FILTER_USE_KEY)
40
        );
41
    }
42
    
43
    /**
44
     * Extending forwarding event to prevent cyclic routing when forwarding under dispatcher events
45
     * - @TODO handle params and other possible route parameters too
46
     * {@inheritDoc}
47
     *
48
     * @param array $route
49
     *
50
     * @return void
51
     */
52
    public function forward(array $forward, $preventCycle = false): void
53
    {
54
        if (!$preventCycle) {
55
            parent::forward($forward);
56
        }
57
        else {
58
            if ((!isset($forward['namespace']) || $this->getNamespaceName() !== $forward['namespace']) &&
0 ignored issues
show
Bug introduced by
It seems like getNamespaceName() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

58
            if ((!isset($forward['namespace']) || $this->/** @scrutinizer ignore-call */ getNamespaceName() !== $forward['namespace']) &&
Loading history...
59
                (!isset($forward['module']) || $this->getModuleName() !== $forward['module']) &&
0 ignored issues
show
Bug introduced by
It seems like getModuleName() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

59
                (!isset($forward['module']) || $this->/** @scrutinizer ignore-call */ getModuleName() !== $forward['module']) &&
Loading history...
60
                (!isset($forward['task']) || $this->getControllerName() !== $forward['task']) &&
0 ignored issues
show
Bug introduced by
It seems like getControllerName() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

60
                (!isset($forward['task']) || $this->/** @scrutinizer ignore-call */ getControllerName() !== $forward['task']) &&
Loading history...
61
                (!isset($forward['controller']) || $this->getControllerName() !== $forward['controller']) &&
62
                (!isset($forward['action']) || $this->getActionName() !== $forward['action']) &&
0 ignored issues
show
Bug introduced by
It seems like getActionName() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

62
                (!isset($forward['action']) || $this->/** @scrutinizer ignore-call */ getActionName() !== $forward['action']) &&
Loading history...
63
                (!isset($forward['params']) || $this->getParams() !== $forward['params'])
0 ignored issues
show
Bug introduced by
It seems like getParams() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

63
                (!isset($forward['params']) || $this->/** @scrutinizer ignore-call */ getParams() !== $forward['params'])
Loading history...
64
            ) {
65
                if (!isset($forward['namespace'])) {
66
                    unset($forward['namespace']);
67
                }
68
                if (!isset($forward['module'])) {
69
                    unset($forward['module']);
70
                }
71
                if (!isset($forward['task'])) {
72
                    unset($forward['task']);
73
                }
74
                if (!isset($forward['controller'])) {
75
                    unset($forward['controller']);
76
                }
77
                if (!isset($forward['action'])) {
78
                    unset($forward['action']);
79
                }
80
                if (!isset($forward['params'])) {
81
                    unset($forward['params']);
82
                }
83
                $this->forward($forward);
84
            }
85
        }
86
    }
87
    
88
    /**
89
     * @return array
90
     */
91
    public function toArray()
92
    {
93
        $ret = [
94
            'namespace' => $this->getNamespaceName(),
95
            'module' => $this->getModuleName(),
96
            'action' => $this->getActionName(),
97
            'params' => $this->getParams(),
98
            'handlerClass' => $this->getHandlerClass(),
0 ignored issues
show
Bug introduced by
It seems like getHandlerClass() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

98
            'handlerClass' => $this->/** @scrutinizer ignore-call */ getHandlerClass(),
Loading history...
99
            'handlerSuffix' => $this->getHandlerSuffix(),
0 ignored issues
show
Bug introduced by
It seems like getHandlerSuffix() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

99
            'handlerSuffix' => $this->/** @scrutinizer ignore-call */ getHandlerSuffix(),
Loading history...
100
            'activeMethod' => $this->getActiveMethod(),
0 ignored issues
show
Bug introduced by
It seems like getActiveMethod() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

100
            'activeMethod' => $this->/** @scrutinizer ignore-call */ getActiveMethod(),
Loading history...
101
            'actionSuffix' => $this->getActionSuffix(),
0 ignored issues
show
Bug introduced by
It seems like getActionSuffix() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

101
            'actionSuffix' => $this->/** @scrutinizer ignore-call */ getActionSuffix(),
Loading history...
102
        ];
103
        if ($this instanceof MvcDispatcher) {
104
            $ret['controller'] = $this->getControllerName();
105
            $ret['previousNamespace'] = $this->getPreviousNamespaceName();
106
            $ret['previousController'] = $this->getPreviousControllerName();
107
            $ret['previousAction'] = $this->getPreviousActionName();
108
        }
109
        if ($this instanceof CliDispatcher) {
110
            $ret['task'] = $this->getTaskName();
111
            $ret['taskSuffix'] = $this->getTaskSuffix();
112
        }
113
        return $ret;
114
    }
115
}
116