Completed
Push — master ( eedef1...85765a )
by Gabriel
02:55
created

Dispatcher::callFromRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 5
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 7
ccs 0
cts 6
cp 0
crap 2
rs 10
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
     * @return void
35
     */
36 4
    public function __construct(Container $container = null)
37
    {
38 4
        if ($container instanceof Container) {
39
            $this->setContainer($container);
40
        }
41 4
    }
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);
0 ignored issues
show
Bug introduced by
It seems like $request can also be of type null; however, parameter $request of Nip\Dispatcher\Commands\...ry::createFromRequest() does only seem to accept Psr\Http\Message\ServerRequestInterface, maybe add an additional type check? ( Ignorable by Annotation )

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

56
        $command = CommandFactory::createFromRequest(/** @scrutinizer ignore-type */ $request);
Loading history...
57
        return $this->dispatchCommand($command)->getReturn();
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->dispatchCommand($command)->getReturn() targeting Nip\Dispatcher\Commands\Command::getReturn() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
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);
0 ignored issues
show
Bug introduced by
It seems like $request can also be of type null; however, parameter $request of Nip\Dispatcher\Commands\...ry::createFromRequest() does only seem to accept Psr\Http\Message\ServerRequestInterface, maybe add an additional type check? ( Ignorable by Annotation )

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

89
        $command = CommandFactory::createFromRequest(/** @scrutinizer ignore-type */ $request);
Loading history...
90
        $command->setActionParam('params', $params);
91
        return $this->getResolverPipeline(InstanceBuilder::class)
92
            ->process($command)
93
            ->getReturn();
94
    }
95
96
    /**
97
     * @param Command $command
98
     * @return Command
99
     */
100 3
    public function dispatchCommand(Command $command)
101
    {
102 3
        $this->getCommandsCollection()[] = $command;
103
104
105
        try {
106 3
            return $this->processCommand($command);
107 1
        } catch (ForwardException $exception) {
108
            $command = CommandFactory::createFromForwardException($exception);
109
            $command->setRequest($this->getRequest());
110
            return $this->dispatchCommand($command);
111
        }
112
    }
113
114
    /**
115
     * @param bool $params
116
     * @throws ForwardException
117
     */
118
    public function throwError($params = false)
0 ignored issues
show
Unused Code introduced by
The parameter $params is not used and could be removed. ( Ignorable by Annotation )

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

118
    public function throwError(/** @scrutinizer ignore-unused */ $params = false)

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

Loading history...
119
    {
120
//        $this->getFrontController()->getTrace()->add($params);
121
        $this->setErrorController();
122
        $this->forward('index');
0 ignored issues
show
Bug introduced by
'index' of type string is incompatible with the type boolean expected by parameter $action of Nip\Dispatcher\Dispatcher::forward(). ( Ignorable by Annotation )

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

122
        $this->forward(/** @scrutinizer ignore-type */ 'index');
Loading history...
123
    }
124
125
    /**
126
     * @return $this
127
     */
128
    public function setErrorController()
129
    {
130
        $this->getRequest()->setActionName('index');
131
        $this->getRequest()->setControllerName('error');
132
        $this->getRequest()->setModuleName('default');
133
134
        return $this;
135
    }
136
137
    /**
138
     * @param bool  $action
139
     * @param bool  $controller
140
     * @param bool  $module
141
     * @param array $params
142
     *
143
     * @throws ForwardException
144
     */
145
    public function forward($action = false, $controller = false, $module = false, $params = [])
146
    {
147
        $this->getRequest()->setActionName($action);
148
149
        if ($controller) {
150
            $this->getRequest()->setControllerName($controller);
0 ignored issues
show
Bug introduced by
$controller of type true is incompatible with the type string expected by parameter $value of Nip\Http\Request::setControllerName(). ( Ignorable by Annotation )

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

150
            $this->getRequest()->setControllerName(/** @scrutinizer ignore-type */ $controller);
Loading history...
151
        }
152
        if ($module) {
153
            $this->getRequest()->setModuleName($module);
0 ignored issues
show
Bug introduced by
$module of type true is incompatible with the type string expected by parameter $value of Nip\Http\Request::setModuleName(). ( Ignorable by Annotation )

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

153
            $this->getRequest()->setModuleName(/** @scrutinizer ignore-type */ $module);
Loading history...
154
        }
155
156
        if (is_array($params)) {
0 ignored issues
show
introduced by
The condition is_array($params) is always true.
Loading history...
157
            $this->getRequest()->attributes->add($params);
158
        }
159
160
        throw new ForwardException();
161
    }
162
163
    /**
164
     * @param $module
165
     * @param $controller
166
     *
167
     * @return string
168
     */
169
    protected function generateFullControllerNameNamespace($module, $controller)
170
    {
171
        $name = app()->get('kernel')->getRootNamespace().'Modules\\';
0 ignored issues
show
Bug introduced by
The function app was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

171
        $name = /** @scrutinizer ignore-call */ app()->get('kernel')->getRootNamespace().'Modules\\';
Loading history...
172
        $module = $module == 'Default' ? 'Frontend' : $module;
173
        $name .= $module.'\Controllers\\';
174
        $name .= str_replace('_', '\\', $controller).'Controller';
175
176
        return $name;
177
    }
178
179
    /**
180
     * @param string $namespaceClass
181
     *
182
     * @return bool
183
     */
184
    protected function isValidControllerNamespace($namespaceClass)
185
    {
186
        return class_exists($namespaceClass);
187
    }
188
189
    /**
190
     * @return \Nip\AutoLoader\AutoLoader
0 ignored issues
show
Bug introduced by
The type Nip\AutoLoader\AutoLoader was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
191
     */
192
    protected function getAutoloader()
193
    {
194
        return app('autoloader');
0 ignored issues
show
Bug introduced by
The function app was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

194
        return /** @scrutinizer ignore-call */ app('autoloader');
Loading history...
195
    }
196
197
    /**
198
     * @param $module
199
     * @param $controller
200
     *
201
     * @return string
202
     */
203
    protected function generateFullControllerNameString($module, $controller)
204
    {
205
        return $module.'_'.$controller.'Controller';
206
    }
207
}
208