ActionCallStage::processCommand()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

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 6
cts 6
cp 1
crap 2
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 Nip\Dispatcher\Resolver\ClassResolver\NameFormatter;
8
use Psr\Http\Message\RequestInterface;
9
use Psr\Http\Message\ResponseInterface;
10
11
/**
12
 * Class ClassInstanceStage
13
 * @package Nip\Dispatcher\Resolver\Pipeline\Stages
14
 */
15
class ActionCallStage extends AbstractStage
16
{
17
    /**
18
     * @return void
19
     * @throws \Exception
20
     */
21 4
    public function processCommand()
22
    {
23 4
        if (!$this->hasInstanceAction()) {
24 1
            throw new InvalidCommandException(
25
                "No valid instance for callback in dispatcher command " .
26 1
                "[" . 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

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