Skip to content

Commit 98e80db

Browse files
committed
Added MessageHelper::makeRedirect()
1 parent 842af0a commit 98e80db

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

src/utils/MessageHelper.php

+27-10
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22

33
namespace Francerz\Http\Utils;
44

5+
use Francerz\Http\Utils\Constants\StatusCodes;
56
use Francerz\Http\Utils\Headers\AbstractAuthorizationHeader;
67
use Francerz\Http\Utils\Headers\BasicAuthorizationHeader;
78
use Francerz\Http\Utils\Headers\BearerAuthorizationHeader;
89
use InvalidArgumentException;
10+
use LogicException;
911
use Psr\Http\Message\MessageInterface;
1012
use Psr\Http\Message\RequestInterface;
1113
use Psr\Http\Message\ResponseInterface;
1214
use Psr\Http\Message\ServerRequestInterface;
15+
use Psr\Http\Message\UriInterface;
1316

1417
class 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

Comments
 (0)