Replaced in_array by contains

This commit is contained in:
Alejandro Celaya
2018-10-05 18:52:42 +02:00
parent ebf2e459e8
commit e55dbef2fc
6 changed files with 45 additions and 27 deletions

View File

@@ -21,7 +21,7 @@ use Zend\Diactoros\Response\JsonResponse;
use Zend\Expressive\Router\RouteResult;
use Zend\I18n\Translator\TranslatorInterface;
use function implode;
use function in_array;
use function Shlinkio\Shlink\Common\contains;
use function sprintf;
class AuthenticationMiddleware implements MiddlewareInterface, StatusCodeInterface, RequestMethodInterface
@@ -72,7 +72,7 @@ class AuthenticationMiddleware implements MiddlewareInterface, StatusCodeInterfa
if ($routeResult === null
|| $routeResult->isFailure()
|| $request->getMethod() === self::METHOD_OPTIONS
|| in_array($routeResult->getMatchedRouteName(), $this->routesWhitelist, true)
|| contains($routeResult->getMatchedRouteName(), $this->routesWhitelist)
) {
return $handler->handle($request);
}

View File

@@ -9,6 +9,15 @@ use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Shlinkio\Shlink\Rest\Exception\RuntimeException;
use function array_shift;
use function explode;
use function json_decode;
use function json_last_error;
use function json_last_error_msg;
use function parse_str;
use function Shlinkio\Shlink\Common\contains;
use function sprintf;
use function trim;
class BodyParserMiddleware implements MiddlewareInterface, RequestMethodInterface
{
@@ -27,17 +36,17 @@ class BodyParserMiddleware implements MiddlewareInterface, RequestMethodInterfac
$currentParams = $request->getParsedBody();
// In requests that do not allow body or if the body has already been parsed, continue to next middleware
if (! empty($currentParams) || \in_array($method, [
if (! empty($currentParams) || contains($method, [
self::METHOD_GET,
self::METHOD_HEAD,
self::METHOD_OPTIONS,
], true)) {
])) {
return $handler->handle($request);
}
// If the accepted content is JSON, try to parse the body from JSON
$contentType = $this->getRequestContentType($request);
if (\in_array($contentType, ['application/json', 'text/json', 'application/x-json'], true)) {
if (contains($contentType, ['application/json', 'text/json', 'application/x-json'])) {
return $handler->handle($this->parseFromJson($request));
}
@@ -51,8 +60,8 @@ class BodyParserMiddleware implements MiddlewareInterface, RequestMethodInterfac
private function getRequestContentType(Request $request): string
{
$contentType = $request->getHeaderLine('Content-type');
$contentTypes = \explode(';', $contentType);
return \trim(\array_shift($contentTypes));
$contentTypes = explode(';', $contentType);
return trim(array_shift($contentTypes));
}
/**
@@ -67,9 +76,9 @@ class BodyParserMiddleware implements MiddlewareInterface, RequestMethodInterfac
return $request;
}
$parsedJson = \json_decode($rawBody, true);
if (\json_last_error() !== JSON_ERROR_NONE) {
throw new RuntimeException(\sprintf('Error when parsing JSON request body: %s', \json_last_error_msg()));
$parsedJson = json_decode($rawBody, true);
if (json_last_error() !== JSON_ERROR_NONE) {
throw new RuntimeException(sprintf('Error when parsing JSON request body: %s', json_last_error_msg()));
}
return $request->withParsedBody($parsedJson);