ActionCallTrait   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 89
Duplicated Lines 0 %

Test Coverage

Coverage 75%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
wmc 10
eloc 25
c 3
b 0
f 0
dl 0
loc 89
rs 10
ccs 21
cts 28
cp 0.75

6 Methods

Rating   Name   Duplication   Size   Complexity  
A callAction() 0 15 3
A dispatchAction() 0 5 1
A runAction() 0 18 3
A validAction() 0 3 1
A getAction() 0 3 1
A setAction() 0 5 1
1
<?php
2
3
namespace Nip\Controllers\Traits;
4
5
use Nip\Dispatcher\Resolver\ClassResolver\NameFormatter;
6
use Nip\Http\Response\Response;
7
use Psr\Http\Message\ResponseInterface;
8
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
9
10
/**
11
 * Trait ActionCallTrait
12
 * @package Nip\Controllers\Traits
13
 */
14
trait ActionCallTrait
15
{
16
    protected $action = null;
17
18
    /**
19
     * @param bool $action
20
     *
21
     * @return Response|ResponseInterface
22
     */
23
    public function dispatchAction($action = false)
24
    {
25
        $action = NameFormatter::formatActionName($action);
26
27
        return $this->callAction($action);
28
    }
29
30
    /**
31
     * @param bool $method
32
     * @param array $parameters
33
     * @return ResponseInterface
34
     */
35 2
    public function callAction($method = false, $parameters = [])
36
    {
37 2
        if ($method) {
38 1
            if ($this->validAction($method)) {
39 1
                $this->setAction($method);
0 ignored issues
show
Bug introduced by
$method of type true is incompatible with the type string expected by parameter $action of Nip\Controllers\Traits\A...nCallTrait::setAction(). ( Ignorable by Annotation )

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

39
                $this->setAction(/** @scrutinizer ignore-type */ $method);
Loading history...
40
41 1
                return $this->runAction($method, $parameters);
42
            } else {
43
                throw new NotFoundHttpException(
44
                    'Controller method ['.$method.'] not found for '.get_class($this)
0 ignored issues
show
Bug introduced by
Are you sure $method of type 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

44
                    'Controller method ['./** @scrutinizer ignore-type */ $method.'] not found for '.get_class($this)
Loading history...
45
                );
46
            }
47
        }
48
49 1
        throw new NotFoundHttpException('No action specified for '.get_class($this));
50
    }
51
52
    /**
53
     * @param $method
54
     * @param array $parameters
55
     * @return ResponseInterface
56
     */
57 1
    protected function runAction($method, $parameters = [])
58
    {
59 1
        $this->invokeStage('parseRequest');
0 ignored issues
show
Bug introduced by
It seems like invokeStage() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

59
        $this->/** @scrutinizer ignore-call */ 
60
               invokeStage('parseRequest');
Loading history...
60 1
        $this->invokeStage('beforeAction');
61
62 1
        $response = call_user_func_array([$this, $method], $parameters);
63
64 1
        if ($response instanceof ResponseInterface) {
65
            $this->setResponse($response);
0 ignored issues
show
Bug introduced by
It seems like setResponse() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

65
            $this->/** @scrutinizer ignore-call */ 
66
                   setResponse($response);
Loading history...
66
        }
67
68 1
        $this->invokeStage('afterAction');
69
70 1
        if ($this->hasResponse()) {
0 ignored issues
show
Bug introduced by
It seems like hasResponse() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

70
        if ($this->/** @scrutinizer ignore-call */ hasResponse()) {
Loading history...
71 1
            return $this->getResponse();
0 ignored issues
show
Bug introduced by
It seems like getResponse() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

71
            return $this->/** @scrutinizer ignore-call */ getResponse();
Loading history...
72
        }
73
74
        return $this->getResponse(true);
75
    }
76
77
    /**
78
     * @param $action
79
     * @return bool
80
     */
81 1
    protected function validAction($action)
82
    {
83 1
        return in_array($action, get_class_methods(get_class($this)));
84
    }
85
86
    /**
87
     * @return string
88
     */
89 1
    public function getAction()
90
    {
91 1
        return $this->action;
92
    }
93
94
    /**
95
     * @param string $action
96
     * @return self
97
     */
98 1
    public function setAction($action)
99
    {
100 1
        $this->action = $action;
101
102 1
        return $this;
103
    }
104
}
105