Completed
Push — master ( d68f4d...84e19d )
by Gabriel
11:04 queued 11s
created

Dispatcher::dispatch()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 0
cts 2
cp 0
rs 9.9666
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 6
1
<?php
2
3
namespace Nip\Dispatcher;
4
5
use Exception;
6
use Nip\Container\Container;
7
use Nip\Container\ContainerAwareTrait;
8
use Nip\Dispatcher\Commands\Command;
9
use Nip\Dispatcher\Commands\CommandFactory;
10
use Nip\Dispatcher\Exceptions\ForwardException;
11
use Nip\Dispatcher\Resolver\HasResolverPipelineTrait;
12
use Nip\Dispatcher\Resolver\Pipeline\InstanceBuilder;
13
use Nip\Dispatcher\Traits\HasCommandsCollection;
14
use Nip\Dispatcher\Traits\HasRequestTrait;
15
use Nip\Request;
16
use Psr\Http\Message\ResponseInterface;
17
18
/**
19
 * Class Dispatcher.
20
 */
21
class Dispatcher
22
{
23
    use ContainerAwareTrait;
24
    use HasResolverPipelineTrait;
25
    use HasRequestTrait;
26
    use HasCommandsCollection;
27
28
    /**
29
     * Create a new controller dispatcher instance.
30
     *
31
     * @param  Container $container
32
     *
33
     *
34 4
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
35
     */
36 4
    public function __construct(Container $container = null)
37
    {
38
        if ($container instanceof Container) {
39 4
            $this->setContainer($container);
40
        }
41
    }
42
43
    /**
44
     * @param Request|null $request
45
     * @return ResponseInterface
46
     * @throws Exception
47
     *
48
     * @return null|Response
49
     */
50
    public function dispatch(Request $request = null)
51
    {
52
        if ($request) {
53
            $this->setRequest($request);
54
        }
55
56
        $command = CommandFactory::createFromRequest($request);
57
        return $this->dispatchCommand($command)->getReturn();
58
    }
59
60
61
    /**
62
     * @param $action
63
     * @param array $params
64
     * @return mixed
65
     * @throws Exception
66
     */
67
    public function call($action, $params = [])
68
    {
69
        $action = is_array($action) ? $action : ['controller' => $action];
70
        $command = CommandFactory::createFromAction($action);
71
        if (isset($params['_request'])) {
72
            $command->setRequest($params['_request']);
73
            unset($params['_request']);
74
        }
75
        $command->setActionParam('params', $params);
76
        return $this->getResolverPipeline(InstanceBuilder::class)
77
            ->process($command)
78
            ->getReturn();
79
    }
80
81
    /**
82
     * @param Request|null $request
83
     * @param array $params
84
     * @return
85
     * @throws Exception
86
     */
87
    public function callFromRequest(Request $request = null, $params = [])
88
    {
89
        $command = CommandFactory::createFromRequest($request);
90
        $command->setActionParam('params', $params);
91
        return $this->getResolverPipeline(InstanceBuilder::class)
92
            ->process($command)
93 3
            ->getReturn();
94
    }
95 3
96
    /**
97
     * @param Command $command
98
     * @return Command
99 3
     */
100 1
    public function dispatchCommand(Command $command)
101
    {
102
        $this->getCommandsCollection()[] = $command;
103
104
105
        try {
106
            return $this->processCommand($command);
107
        } catch (ForwardException $exception) {
108
            $command = CommandFactory::createFromForwardExecption($exception);
109
            $return = $this->dispatchCommand($command);
110
111
            return $return;
112
        }
113
    }
114
115
    /**
116
     * @param bool $params
117
     * @throws ForwardException
118
     */
119
    public function throwError($params = false)
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
120
    {
121
//        $this->getFrontController()->getTrace()->add($params);
122
        $this->setErrorController();
123
        $this->forward('index');
0 ignored issues
show
Documentation introduced by
'index' is of type string, but the function expects a boolean.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
124
    }
125
126
    /**
127
     * @return $this
128
     */
129
    public function setErrorController()
130
    {
131
        $this->getRequest()->setActionName('index');
132
        $this->getRequest()->setControllerName('error');
133
        $this->getRequest()->setModuleName('default');
134
135
        return $this;
136
    }
137
138
    /**
139
     * @param bool  $action
140
     * @param bool  $controller
141
     * @param bool  $module
142
     * @param array $params
143
     *
144
     * @throws ForwardException
145
     */
146
    public function forward($action = false, $controller = false, $module = false, $params = [])
147
    {
148
        $this->getRequest()->setActionName($action);
0 ignored issues
show
Documentation introduced by
$action is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
149
150
        if ($controller) {
151
            $this->getRequest()->setControllerName($controller);
0 ignored issues
show
Documentation introduced by
$controller is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
152
        }
153
        if ($module) {
154
            $this->getRequest()->setModuleName($module);
0 ignored issues
show
Documentation introduced by
$module is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
155
        }
156
157
        if (is_array($params)) {
158
            $this->getRequest()->attributes->add($params);
159
        }
160
161
        throw new ForwardException();
162
    }
163
164
    /**
165
     * @param $module
166
     * @param $controller
167
     *
168
     * @return string
169
     */
170
    protected function generateFullControllerNameNamespace($module, $controller)
171
    {
172
        $name = app()->get('kernel')->getRootNamespace().'Modules\\';
173
        $module = $module == 'Default' ? 'Frontend' : $module;
174
        $name .= $module.'\Controllers\\';
175
        $name .= str_replace('_', '\\', $controller).'Controller';
176
177
        return $name;
178
    }
179
180
    /**
181
     * @param string $namespaceClass
182
     *
183
     * @return bool
184
     */
185
    protected function isValidControllerNamespace($namespaceClass)
186
    {
187
        return class_exists($namespaceClass);
188
    }
189
190
    /**
191
     * @return \Nip\AutoLoader\AutoLoader
192
     */
193
    protected function getAutoloader()
194
    {
195
        return app('autoloader');
196
    }
197
198
    /**
199
     * @param $module
200
     * @param $controller
201
     *
202
     * @return string
203
     */
204
    protected function generateFullControllerNameString($module, $controller)
205
    {
206
        return $module.'_'.$controller.'Controller';
207
    }
208
}
209