MethodCallStage   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 1 Features 2
Metric Value
eloc 14
c 3
b 1
f 2
dl 0
loc 34
ccs 0
cts 15
cp 0
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A processCommand() 0 11 2
A callMethod() 0 11 3
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