22
33namespace Francerz \Http \Utils ;
44
5+ use Francerz \Http \Utils \Constants \StatusCodes ;
56use Francerz \Http \Utils \Headers \AbstractAuthorizationHeader ;
67use Francerz \Http \Utils \Headers \BasicAuthorizationHeader ;
78use Francerz \Http \Utils \Headers \BearerAuthorizationHeader ;
89use InvalidArgumentException ;
10+ use LogicException ;
911use Psr \Http \Message \MessageInterface ;
1012use Psr \Http \Message \RequestInterface ;
1113use Psr \Http \Message \ResponseInterface ;
1214use Psr \Http \Message \ServerRequestInterface ;
15+ use Psr \Http \Message \UriInterface ;
1316
1417class MessageHelper
1518{
@@ -20,14 +23,19 @@ public static function setHttpFactoryManager(HttpFactoryManager $factories)
2023 static ::$ httpFactoryManager = $ factories ;
2124 }
2225
23- public static function getCurrentRequest () : ServerRequestInterface
26+ private static function checkFactoryManager ( $ method )
2427 {
2528 if (!isset (static ::$ httpFactoryManager )) {
26- throw new \ Exception (sprintf (
27- 'Method %s::%s requires you setHttpFactoryManager on %s. ' ,
28- __CLASS__ , __METHOD__ , __CLASS__
29+ throw new LogicException (sprintf (
30+ 'Method %s requires assign setHttpFactoryManager ' ,
31+ $ method
2932 ));
3033 }
34+ }
35+
36+ public static function getCurrentRequest () : ServerRequestInterface
37+ {
38+ static ::checkFactoryManager (__METHOD__ );
3139
3240 $ requestFactory = static ::$ httpFactoryManager ->getServerRequestFactory ();
3341 $ uriFactory = static ::$ httpFactoryManager ->getUriFactory ();
@@ -124,12 +132,7 @@ public static function getContent(MessageInterface $message)
124132
125133 public static function withContent (MessageInterface $ message , string $ mediaType , $ content ) : MessageInterface
126134 {
127- if (!isset (static ::$ httpFactoryManager )) {
128- throw new \Exception (sprintf (
129- 'Method %s::%s requires you setHttpFactoryManager on %s. ' ,
130- __CLASS__ , __METHOD__ , __CLASS__
131- ));
132- }
135+ static ::checkFactoryManager (__METHOD__ );
133136
134137 $ parser = BodyParserHandler::find ($ mediaType );
135138 $ streamFactory = static ::$ httpFactoryManager ->getStreamFactory ();
@@ -145,6 +148,20 @@ public static function withContent(MessageInterface $message, string $mediaType,
145148 ->withHeader ('Content-Type ' , $ mediaType );
146149 }
147150
151+ public static function makeRedirect ($ location , int $ code = StatusCodes::REDIRECT_TEMPORARY_REDIRECT )
152+ {
153+ static ::checkFactoryManager (__METHOD__ );
154+ $ responseFactory = static ::$ httpFactoryManager ->getResponseFactory ();
155+
156+ if ($ location instanceof UriInterface) {
157+ $ location = (string )$ location ;
158+ }
159+
160+ return $ responseFactory
161+ ->createResponse ($ code )
162+ ->withHeader ('Location ' , $ location );
163+ }
164+
148165 public static function isInfo (ResponseInterface $ response ) : bool
149166 {
150167 return $ response ->getStatusCode () >= 100 && $ response ->getStatusCode () < 200 ;
0 commit comments