Test Setup Failed
Push — master ( 20fe9d...56570e )
by Gabriel
01:51
created

Dispatcher::throwError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 2
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 mixed
61
     * @throws Exception
62
     */
63
    public function call($action, $params = [])
64
    {
65
        $action = is_array($action) ? $action : ['controller' => $action];
66
        $command = CommandFactory::createFromAction($action);
67
        if (isset($params['_request'])) {
68
            $command->setRequest($params['_request']);
69
            unset($params['_request']);
70
        }
71
        $command->setActionParam('params', $params);
72
        return $this->getResolverPipeline(InstanceBuilder::class)
73
            ->process($command)
74
            ->getReturn();
75
    }
76
77
    /**
78
     * @param Request|null $request
79
     * @return
80
     */
81
    public function callFromRequest(Request $request = null)
82
    {
83
        $command = CommandFactory::createFromRequest($request);
84
        return $this->getResolverPipeline(InstanceBuilder::class)
85
            ->process($command)
86
            ->getReturn();
87
    }
88
89
    /**
90
     * @param Command $command
91
     * @return Command
92
     */
93 3
    public function dispatchCommand(Command $command)
94
    {
95 3
        $this->getCommandsCollection()[] = $command;
96
97
98
        try {
99 3
            return $this->processCommand($command);
100 1
        } catch (ForwardException $exception) {
101
            $command = CommandFactory::createFromForwardExecption($exception);
102
            $return = $this->dispatchCommand($command);
103
104
            return $return;
105
        }
106
    }
107
108
    /**
109
     * @param bool $params
110
     */
111
    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...
112
    {
113
//        $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...
114
        $this->setErrorController();
115
        $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...
116
117
        return;
118
    }
119
120
    /**
121
     * @return $this
122
     */
123
    public function setErrorController()
124
    {
125
        $this->getRequest()->setActionName('index');
126
        $this->getRequest()->setControllerName('error');
127
        $this->getRequest()->setModuleName('default');
128
129
        return $this;
130
    }
131
132
    /**
133
     * @param bool $action
134
     * @param bool $controller
135
     * @param bool $module
136
     * @param array $params
137
     * @throws ForwardException
138
     */
139
    public function forward($action = false, $controller = false, $module = false, $params = [])
140
    {
141
        $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...
142
143
        if ($controller) {
144
            $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...
145
        }
146
        if ($module) {
147
            $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...
148
        }
149
150
        if (is_array($params)) {
151
            $this->getRequest()->attributes->add($params);
152
        }
153
154
        throw new ForwardException;
155
    }
156
}
157