ActionCallStage::invokeAction()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2.0078

Importance

Changes 2
Bugs 0 Features 2
Metric Value
cc 2
eloc 7
c 2
b 0
f 2
nc 2
nop 0
dl 0
loc 11
ccs 7
cts 8
cp 0.875
crap 2.0078
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