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
![]() |
|||||||
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
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
![]() |
|||||||
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
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
![]() |
|||||||
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
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
![]() |
|||||||
66 | } |
||||||
67 | |||||||
68 | 1 | $this->invokeStage('afterAction'); |
|||||
69 | |||||||
70 | 1 | if ($this->hasResponse()) { |
|||||
0 ignored issues
–
show
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
![]() |
|||||||
71 | 1 | return $this->getResponse(); |
|||||
0 ignored issues
–
show
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
![]() |
|||||||
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 |