Test Setup Failed
Push — master ( 251f8c...20fe9d )
by Gabriel
05:53
created

Dispatcher::dispatchCommand()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.3149

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 4
cts 7
cp 0.5714
rs 9.7998
c 0
b 0
f 0
cc 2
nc 2
nop 1
crap 2.3149
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
17
/**
18
 * Class Dispatcher
19
 * @package Nip\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
     * @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...
33
     */
34 4
    public function __construct(Container $container = null)
35
    {
36 4
        if ($container instanceof Container) {
37
            $this->setContainer($container);
38
        }
39 4
    }
40
41
    /**
42
     * @param Request|null $request
43
     * @return \Psr\Http\Message\ResponseInterface
44
     * @throws Exception
45
     */
46
    public function dispatch(Request $request = null)
47
    {
48
        if ($request) {
49
            $this->setRequest($request);
50
        }
51
52
        $command = CommandFactory::createFromRequest($request);
53
        return $this->dispatchCommand($command)->getReturn();
54
    }
55
56
57
    /**
58
     * @param $action
59
     * @param array $params
60
     * @return
61
     */
62
    public function call($action, $params = [])
63
    {
64
        $action = is_array($action) ? $action : ['controller' => $action];
65
        $action['params'] = $params;
66
        $command = CommandFactory::createFromAction($action);
67
        return $this->getResolverPipeline(InstanceBuilder::class)
68
            ->process($command)
69
            ->getReturn();
70
    }
71
72
    /**
73
     * @param Request|null $request
74
     * @return
75
     */
76
    public function callFromRequest(Request $request = null)
77
    {
78
        $command = CommandFactory::createFromRequest($request);
79
        return $this->getResolverPipeline(InstanceBuilder::class)
80
            ->process($command)
81
            ->getReturn();
82
    }
83
84
    /**
85
     * @param Command $command
86
     * @return Command
87
     */
88 3
    public function dispatchCommand(Command $command)
89
    {
90 3
        $this->getCommandsCollection()[] = $command;
91
92
93
        try {
94 3
            return $this->processCommand($command);
95 1
        } catch (ForwardException $exception) {
96
            $command = CommandFactory::createFromForwardExecption($exception);
97
            $return = $this->dispatchCommand($command);
98
99
            return $return;
100
        }
101
    }
102
103
    /**
104
     * @param bool $params
105
     */
106
    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...
107
    {
108
//        $this->getFrontController()->getTrace()->add($params);
0 ignored issues
show
Unused Code Comprehensibility introduced by
75% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
109
        $this->setErrorController();
110
        $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...
111
112
        return;
113
    }
114
115
    /**
116
     * @return $this
117
     */
118
    public function setErrorController()
119
    {
120
        $this->getRequest()->setActionName('index');
121
        $this->getRequest()->setControllerName('error');
122
        $this->getRequest()->setModuleName('default');
123
124
        return $this;
125
    }
126
127
    /**
128
     * @param bool $action
129
     * @param bool $controller
130
     * @param bool $module
131
     * @param array $params
132
     * @throws ForwardException
133
     */
134
    public function forward($action = false, $controller = false, $module = false, $params = [])
135
    {
136
        $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...
137
138
        if ($controller) {
139
            $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...
140
        }
141
        if ($module) {
142
            $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...
143
        }
144
145
        if (is_array($params)) {
146
            $this->getRequest()->attributes->add($params);
147
        }
148
149
        throw new ForwardException;
150
    }
151
}
152