11
11
use NilPortugues \Api \JsonApi \Http \Response \Response ;
12
12
use NilPortugues \Api \JsonApi \Http \Response \UnprocessableEntity ;
13
13
use NilPortugues \Api \JsonApi \Http \Response \UnsupportedAction ;
14
+ use NilPortugues \Api \JsonApi \Server \Errors \Error ;
15
+ use NilPortugues \Api \JsonApi \Server \Errors \ErrorBag ;
14
16
use Psr \Http \Message \ResponseInterface ;
15
17
use Symfony \Bridge \PsrHttpMessage \Factory \HttpFoundationFactory ;
16
18
@@ -33,8 +35,9 @@ protected function addHeaders(ResponseInterface $response)
33
35
*/
34
36
protected function errorResponse ($ json )
35
37
{
36
- return (new HttpFoundationFactory ())
37
- ->createResponse ($ this ->addHeaders (new BadRequest ($ json )));
38
+ $ error = new Error ('Bad Request ' , json_decode ($ json ));
39
+
40
+ return $ this ->createResponse (new BadRequest (new ErrorBag ([$ error ])));
38
41
}
39
42
40
43
/**
@@ -44,8 +47,7 @@ protected function errorResponse($json)
44
47
*/
45
48
protected function resourceCreatedResponse ($ json )
46
49
{
47
- return (new HttpFoundationFactory ())
48
- ->createResponse ($ this ->addHeaders (new ResourceCreated ($ json )));
50
+ return $ this ->createResponse (new ResourceCreated ($ json ));
49
51
}
50
52
51
53
/**
@@ -55,8 +57,7 @@ protected function resourceCreatedResponse($json)
55
57
*/
56
58
protected function resourceDeletedResponse ($ json )
57
59
{
58
- return (new HttpFoundationFactory ())
59
- ->createResponse ($ this ->addHeaders (new ResourceDeleted ($ json )));
60
+ return $ this ->createResponse (new ResourceDeleted ($ json ));
60
61
}
61
62
62
63
/**
@@ -66,8 +67,7 @@ protected function resourceDeletedResponse($json)
66
67
*/
67
68
protected function resourceNotFoundResponse ($ json )
68
69
{
69
- return (new HttpFoundationFactory ())
70
- ->createResponse ($ this ->addHeaders (new ResourceNotFound ($ json )));
70
+ return $ this ->createResponse (new ResourceNotFound ($ json ));
71
71
}
72
72
73
73
/**
@@ -77,8 +77,9 @@ protected function resourceNotFoundResponse($json)
77
77
*/
78
78
protected function resourcePatchErrorResponse ($ json )
79
79
{
80
- return (new HttpFoundationFactory ())
81
- ->createResponse ($ this ->addHeaders (new UnprocessableEntity ($ json )));
80
+ $ error = new Error ('Unprocessable Entity ' , json_decode ($ json ));
81
+
82
+ return $ this ->createResponse (new UnprocessableEntity ([$ error ]));
82
83
}
83
84
84
85
/**
@@ -88,8 +89,9 @@ protected function resourcePatchErrorResponse($json)
88
89
*/
89
90
protected function resourcePostErrorResponse ($ json )
90
91
{
91
- return (new HttpFoundationFactory ())
92
- ->createResponse ($ this ->addHeaders (new UnprocessableEntity ($ json )));
92
+ $ error = new Error ('Unprocessable Entity ' , json_decode ($ json ));
93
+
94
+ return $ this ->createResponse (new UnprocessableEntity ([$ error ]));
93
95
}
94
96
95
97
/**
@@ -99,8 +101,7 @@ protected function resourcePostErrorResponse($json)
99
101
*/
100
102
protected function resourceProcessingResponse ($ json )
101
103
{
102
- return (new HttpFoundationFactory ())
103
- ->createResponse ($ this ->addHeaders (new ResourceProcessing ($ json )));
104
+ return $ this ->createResponse (new ResourceProcessing ($ json ));
104
105
}
105
106
106
107
/**
@@ -110,8 +111,7 @@ protected function resourceProcessingResponse($json)
110
111
*/
111
112
protected function resourceUpdatedResponse ($ json )
112
113
{
113
- return (new HttpFoundationFactory ())
114
- ->createResponse ($ this ->addHeaders (new ResourceUpdated ($ json )));
114
+ return $ this ->createResponse (new ResourceUpdated ($ json ));
115
115
}
116
116
117
117
/**
@@ -121,8 +121,7 @@ protected function resourceUpdatedResponse($json)
121
121
*/
122
122
protected function response ($ json )
123
123
{
124
- return (new HttpFoundationFactory ())
125
- ->createResponse ($ this ->addHeaders (new Response ($ json )));
124
+ return $ this ->createResponse (new Response ($ json ));
126
125
}
127
126
128
127
/**
@@ -132,7 +131,18 @@ protected function response($json)
132
131
*/
133
132
protected function unsupportedActionResponse ($ json )
134
133
{
135
- return (new HttpFoundationFactory ())
136
- ->createResponse ($ this ->addHeaders (new UnsupportedAction ($ json )));
134
+ $ error = new Error ('Unsupported Action ' , json_decode ($ json ));
135
+
136
+ return $ this ->createResponse (new UnsupportedAction ([$ error ]));
137
+ }
138
+
139
+ /**
140
+ * @param $data
141
+ *
142
+ * @return \Symfony\Component\HttpFoundation\Response
143
+ */
144
+ private function createResponse ($ data )
145
+ {
146
+ return (new HttpFoundationFactory ())->createResponse ($ this ->addHeaders ($ data ));
137
147
}
138
148
}
0 commit comments