44
55namespace SimpleAsFuck \ApiToolkit \Factory \Server ;
66
7- use GuzzleHttp \Psr7 \HttpFactory ;
87use GuzzleHttp \Psr7 \PumpStream ;
8+ use GuzzleHttp \Psr7 \Utils ;
99use Kayex \HttpCodes ;
10- use Psr \Http \Message \ResponseFactoryInterface ;
1110use Psr \Http \Message \ResponseInterface ;
1211use SimpleAsFuck \ApiToolkit \Model \Webhook \Result ;
1312use SimpleAsFuck \ApiToolkit \Service \Transformation \NotNull ;
1817final class ResponseFactory
1918{
2019 /**
20+ * @param int<100,505> $code
21+ * @param array<non-empty-string, string|array<string>> $headers
22+ */
23+ public static function make (int $ code = HttpCodes::HTTP_OK , array $ headers = []): ResponseInterface
24+ {
25+ return new \GuzzleHttp \Psr7 \Response ($ code , $ headers );
26+ }
27+
28+ /**
29+ * @todo 0.9 $body and $transformer change to not null
2130 * @template TBody
2231 * @param TBody|null $body will be encoded as application/json object
2332 * @param Transformer<TBody>|null $transformer
@@ -27,10 +36,9 @@ final class ResponseFactory
2736 public static function makeObject (mixed $ body , ?Transformer $ transformer = null , int $ code = HttpCodes::HTTP_OK , array $ headers = []): ResponseInterface
2837 {
2938 $ headers ['Content-Type ' ] = 'application/json ' ;
30- $ factory = new HttpFactory ();
31- $ response = self ::makeResponse ($ factory , $ code , $ headers );
39+ $ response = self ::make ($ code , $ headers );
3240
33- return $ response ->withBody ($ factory -> createStream (\json_encode (Nullable ::toApi ($ body , $ transformer ), \JSON_THROW_ON_ERROR )));
41+ return $ response ->withBody (Utils :: streamFor (\json_encode (Nullable ::toApi ($ body , $ transformer ), \JSON_THROW_ON_ERROR )));
3442 }
3543
3644 /**
@@ -43,8 +51,7 @@ public static function makeObject(mixed $body, ?Transformer $transformer = null,
4351 public static function makeArray (\Iterator $ body , ?Transformer $ transformer = null , int $ code = HttpCodes::HTTP_OK , array $ headers = []): ResponseInterface
4452 {
4553 $ headers ['Content-Type ' ] = 'application/json ' ;
46- $ factory = new HttpFactory ();
47- $ response = self ::makeResponse ($ factory , $ code , $ headers );
54+ $ response = self ::make ($ code , $ headers );
4855
4956 $ start = true ;
5057 return $ response ->withBody (new PumpStream (function () use ($ body , $ transformer , &$ start ): ?string {
@@ -83,8 +90,7 @@ public static function makeArray(\Iterator $body, ?Transformer $transformer = nu
8390 public static function makeArrayAssoc (\Iterator $ body , ?Transformer $ transformer = null , int $ code = HttpCodes::HTTP_OK , array $ headers = []): ResponseInterface
8491 {
8592 $ headers ['Content-Type ' ] = 'application/json ' ;
86- $ factory = new HttpFactory ();
87- $ response = self ::makeResponse ($ factory , $ code , $ headers );
93+ $ response = self ::make ($ code , $ headers );
8894
8995 $ start = true ;
9096 return $ response ->withBody (new PumpStream (function () use ($ body , $ transformer , &$ start ): ?string {
@@ -123,8 +129,7 @@ public static function makeArrayAssoc(\Iterator $body, ?Transformer $transformer
123129 public static function makeStream (\Iterator $ body , ?Transformer $ transformer = null , int $ code = HttpCodes::HTTP_OK , array $ headers = []): ResponseInterface
124130 {
125131 $ headers ['Content-Type ' ] = 'application/jsonl ' ;
126- $ factory = new HttpFactory ();
127- $ response = self ::makeResponse ($ factory , $ code , $ headers );
132+ $ response = self ::make ($ code , $ headers );
128133
129134 return $ response ->withBody (new PumpStream (static function () use ($ body , $ transformer ): ?string {
130135 if (! $ body ->valid ()) {
@@ -144,19 +149,4 @@ public static function makeWebhookResult(bool $stopDispatching = false, array $h
144149 {
145150 return self ::makeObject (new Result ($ stopDispatching ), new ResultTransformer (), HttpCodes::HTTP_OK , $ headers );
146151 }
147-
148- /**
149- * @param int<100,505> $code
150- * @param array<non-empty-string, string|array<string>> $headers
151- */
152- private static function makeResponse (ResponseFactoryInterface $ factory , int $ code , array $ headers ): ResponseInterface
153- {
154- $ response = $ factory ->createResponse ($ code );
155-
156- foreach ($ headers as $ name => $ header ) {
157- $ response = $ response ->withHeader ($ name , $ header );
158- }
159-
160- return $ response ;
161- }
162152}
0 commit comments