ActionCallStage   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 93.33%

Importance

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

2 Methods

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