1 | <?php |
||||||
2 | |||||||
3 | declare(strict_types=1); |
||||||
4 | |||||||
5 | namespace Nip\Controllers\Response; |
||||||
6 | |||||||
7 | use Nip\Controllers\Controller; |
||||||
8 | use Nip\Controllers\Traits\HasResponseTrait; |
||||||
9 | use Nip\Controllers\Traits\HasViewTrait; |
||||||
10 | use Nip\Controllers\View\ControllerViewHydrator; |
||||||
11 | use Nip\Http\Response\JsonResponse; |
||||||
12 | use Nip\Http\Response\Response; |
||||||
13 | use Nip\Utility\Str; |
||||||
14 | |||||||
15 | /** |
||||||
16 | * Class ResponsePayloadTransformer |
||||||
17 | * @package Nip\Controllers\Response |
||||||
18 | */ |
||||||
19 | class ResponsePayloadTransformer |
||||||
20 | { |
||||||
21 | /** |
||||||
22 | * @var Controller|HasViewTrait |
||||||
23 | */ |
||||||
24 | protected $controller; |
||||||
25 | |||||||
26 | protected $payload; |
||||||
27 | protected $factory; |
||||||
28 | |||||||
29 | /** |
||||||
30 | * @param Controller|HasResponseTrait $controller |
||||||
31 | 1 | * @param ResponsePayload $payload |
|||||
32 | * @return Response |
||||||
33 | 1 | */ |
|||||
34 | public static function make($controller, ResponsePayload $payload) |
||||||
35 | 1 | { |
|||||
36 | $transformer = new static($controller, $payload); |
||||||
37 | |||||||
38 | return $transformer->toResponse(); |
||||||
0 ignored issues
–
show
Bug
Best Practice
introduced
by
![]() |
|||||||
39 | } |
||||||
40 | |||||||
41 | /** |
||||||
42 | * ResponsePayloadTransformer constructor. |
||||||
43 | 1 | * @param Controller $controller |
|||||
44 | * @param ResponsePayload $payload |
||||||
45 | 1 | */ |
|||||
46 | 1 | protected function __construct($controller, ResponsePayload $payload) |
|||||
47 | { |
||||||
48 | 1 | $this->controller = $controller; |
|||||
49 | 1 | $this->payload = $payload; |
|||||
50 | |||||||
51 | $this->factory = new ResponseFactory(); |
||||||
52 | } |
||||||
53 | |||||||
54 | |||||||
55 | 1 | /** |
|||||
56 | * @return Response|JsonResponse |
||||||
57 | 1 | */ |
|||||
58 | 1 | protected function toResponse() |
|||||
59 | { |
||||||
60 | $controllerMethod = $this->responseControllerMethod(); |
||||||
61 | 1 | if (method_exists($this->controller, $controllerMethod)) { |
|||||
62 | 1 | return call_user_func_array([$this->controller, $controllerMethod], [$this->factory, $this->payload]); |
|||||
63 | } |
||||||
64 | $format = $this->payload->getFormat(); |
||||||
65 | switch ($format) { |
||||||
66 | 1 | case 'view': |
|||||
67 | case 'html': |
||||||
68 | case 'modal': |
||||||
69 | return $this->toViewResponse($format); |
||||||
70 | case 'json': |
||||||
71 | return $this->factory->json($this->payload->all()); |
||||||
72 | } |
||||||
73 | |||||||
74 | return $this->factory->noContent(); |
||||||
75 | } |
||||||
76 | |||||||
77 | /** |
||||||
78 | * @return Response |
||||||
79 | */ |
||||||
80 | protected function toViewResponse($format): Response |
||||||
81 | { |
||||||
82 | $view = $this->controller->getView(); |
||||||
0 ignored issues
–
show
The method
getView() does not exist on Nip\Controllers\Controller . Since you implemented __call , consider adding a @method annotation.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
83 | |||||||
84 | ControllerViewHydrator::populatePath($view, $this->controller); |
||||||
0 ignored issues
–
show
It seems like
$this->controller can also be of type Nip\Controllers\Traits\HasViewTrait ; however, parameter $controller of Nip\Controllers\View\Con...ydrator::populatePath() does only seem to accept Nip\Controllers\Controller , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
85 | ControllerViewHydrator::initVars($view, $this->controller); |
||||||
0 ignored issues
–
show
It seems like
$this->controller can also be of type Nip\Controllers\Traits\HasViewTrait ; however, parameter $controller of Nip\Controllers\View\Con...iewHydrator::initVars() does only seem to accept Nip\Controllers\Controller|null , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
86 | ControllerViewHydrator::initContentBlocks($view, $this->controller); |
||||||
0 ignored issues
–
show
It seems like
$view can also be of type boolean ; however, parameter $view of Nip\Controllers\View\Con...or::initContentBlocks() does only seem to accept Nip\View\View , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
87 | |||||||
88 | $this->factory->setView($view); |
||||||
0 ignored issues
–
show
It seems like
$view can also be of type boolean ; however, parameter $view of Nip\Controllers\Response...ponseFactory::setView() does only seem to accept Nip\View\View|null , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
89 | $this->payload->headers->set('Content-Type', 'text/html'); |
||||||
90 | |||||||
91 | $viewPath = $format == 'modal' ? '/' . trim($view->getBlock('content'), '/') : $this->controller->getLayoutPath( |
||||||
0 ignored issues
–
show
The method
getLayoutPath() does not exist on Nip\Controllers\Controller . Since you implemented __call , consider adding a @method annotation.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() It seems like
$view->getBlock('content') can also be of type null ; however, parameter $string of trim() does only seem to accept string , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||||
92 | ); |
||||||
93 | |||||||
94 | $response = $this->factory->view( |
||||||
95 | $viewPath, |
||||||
96 | 1 | $this->payload->data->all(), |
|||||
97 | Response::HTTP_OK, |
||||||
98 | 1 | $this->payload->headers->all() |
|||||
99 | 1 | ); |
|||||
100 | 1 | ||||||
101 | $response->setCharset('utf-8'); |
||||||
102 | return $response; |
||||||
103 | } |
||||||
104 | |||||||
105 | protected function responseControllerMethod(): string |
||||||
106 | { |
||||||
107 | return $this->controller->getAction() |
||||||
108 | . Str::studly($this->payload->getFormat()) |
||||||
109 | . 'Response'; |
||||||
110 | } |
||||||
111 | } |
||||||
112 |