MethodCallStage::processCommand()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 6
c 1
b 0
f 1
nc 2
nop 0
dl 0
loc 11
ccs 0
cts 6
cp 0
crap 6
rs 10
1
<?php
2
3
namespace Nip\Dispatcher\Resolver\Pipeline\Stages;
4
5
use Nip\Controllers\Controller;
6
use Nip\Dispatcher\Exceptions\InvalidCommandException;
7
use Psr\Http\Message\RequestInterface;
8
use Psr\Http\Message\ResponseInterface;
9
10
/**
11
 * Class MethodCallStage
12
 * @package Nip\Dispatcher\Resolver\Pipeline\Stages
13
 */
14
class MethodCallStage extends AbstractStage
15
{
16
    /**
17
     * @return void
18
     * @throws \Exception
19
     */
20
    public function processCommand()
21
    {
22
        if (!$this->hasInstanceAction()) {
23
            throw new InvalidCommandException(
24
                "No valid instance for callback in dispatcher command " .
25
                "[" . print_r($this->getCommand(), true) . "]"
0 ignored issues
show
Bug introduced by
Are you sure print_r($this->getCommand(), true) of type string|true can be used in concatenation? ( Ignorable by Annotation )

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

25
                "[" . /** @scrutinizer ignore-type */ print_r($this->getCommand(), true) . "]"
Loading history...
26
            );
27
        }
28
29
        $return = $this->callMethod();
30
        $this->getCommand()->setReturn($return);
31
    }
32
33
    /**
34
     * @return ResponseInterface
35
     * @throws \Exception
36
     */
37
    protected function callMethod()
38
    {
39
        $command = $this->getCommand();
40
        /** @var Controller $controllerInstance */
41
        $controllerInstance = $this->getCommand()->getActionParam('instance');
42
        if ($this->getCommand()->hasRequest()) {
43
            $controllerInstance->setRequest($this->getCommand()->getRequest());
44
        }
45
        $method = $command->getActionParam('action');
46
        $params = $command->hasActionParam('params') ? $command->getActionParam('params') : [];
47
        return $controllerInstance->{$method}(...$params);
48
    }
49
}
50