Skip to content

Commit

Permalink
refactor:
Browse files Browse the repository at this point in the history
  • Loading branch information
apple-x-co committed Aug 16, 2024
1 parent 6306c64 commit e38877f
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 67 deletions.
2 changes: 1 addition & 1 deletion source/app/src/DiBinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
use Aura\SqlQuery\QueryFactory;
use Koriym\QueryLocator\QueryLocator;
use Koriym\QueryLocator\QueryLocatorInterface;
use Laminas\Diactoros\ServerRequestFactory;
use MyVendor\MyPackage\Auth\AdminAuthenticationHandler;
use MyVendor\MyPackage\Auth\AdminAuthenticator;
use MyVendor\MyPackage\Auth\AdminAuthenticatorInterface;
Expand All @@ -44,6 +43,7 @@
use MyVendor\MyPackage\Responder\WebResponder;
use MyVendor\MyPackage\Router\CliRouter;
use MyVendor\MyPackage\Router\RouterInterface;
use MyVendor\MyPackage\Router\ServerRequestFactory;
use MyVendor\MyPackage\Router\WebRouter;
use MyVendor\MyPackage\TemplateEngine\QiqCustomHelper;
use MyVendor\MyPackage\TemplateEngine\QiqRenderer;
Expand Down
73 changes: 73 additions & 0 deletions source/app/src/Router/ServerRequestFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

declare(strict_types=1);

namespace MyVendor\MyPackage\Router;

use Laminas\Diactoros\ServerRequestFactory as LaminasServerRequestFactory;
use Psr\Http\Message\ServerRequestInterface;

use function assert;
use function in_array;
use function is_array;
use function json_decode;
use function json_last_error;
use function json_last_error_msg;
use function parse_str;

use const JSON_ERROR_NONE;
use const JSON_THROW_ON_ERROR;

final class ServerRequestFactory
{
public static function fromGlobals(): ServerRequestInterface
{
$serverRequest = LaminasServerRequestFactory::fromGlobals();

$isFormUrlEncoded = in_array(
'application/x-www-form-urlencoded',
$serverRequest->getHeader('content-type'),
true,
);
if ($isFormUrlEncoded) {
return self::parseFormUrlEncoded($serverRequest);
}

$isJson = in_array(
'application/json',
$serverRequest->getHeader('content-type'),
true,
);
if (! $isJson) {
return $serverRequest;
}

return self::parseJson($serverRequest);
}

private static function parseFormUrlEncoded(ServerRequestInterface $serverRequest): ServerRequestInterface
{
parse_str((string) $serverRequest->getBody(), $parsedBody);

return $serverRequest->withParsedBody($parsedBody);
}

private static function parseJson(ServerRequestInterface $serverRequest): ServerRequestInterface
{
$parsedBody = json_decode(
(string) $serverRequest->getBody(),
true,
512,
JSON_THROW_ON_ERROR,
);

$error = json_last_error();
if ($error !== JSON_ERROR_NONE) {
throw new InvalidRequestException(json_last_error_msg());
}

assert(is_array($parsedBody));

return $serverRequest->withParsedBody($parsedBody);
}
}
67 changes: 1 addition & 66 deletions source/app/src/Router/WebRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,6 @@
use Aura\Router\RouterContainer;
use Psr\Http\Message\ServerRequestInterface;

use function assert;
use function in_array;
use function is_array;
use function json_decode;
use function json_last_error;
use function json_last_error_msg;
use function parse_str;

use const JSON_ERROR_NONE;
use const JSON_THROW_ON_ERROR;

final class WebRouter implements RouterInterface
{
public function __construct(
Expand All @@ -30,66 +19,12 @@ public function match(ServerRequestInterface $serverRequest): RouterMatch
{
$matcher = $this->routerContainer->getMatcher();

$isFormUrlEncoded = in_array(
'application/x-www-form-urlencoded',
$serverRequest->getHeader('content-type'),
true,
);
if ($isFormUrlEncoded) {
return new RouterMatch(
$serverRequest->getMethod(),
$serverRequest->getUri()->getPath(),
$matcher->match($serverRequest),
$this->parseFormUrlEncoded($serverRequest),
);
}

$isJson = in_array(
'application/json',
$serverRequest->getHeader('content-type'),
true,
);
if (! $isJson) {
return new RouterMatch(
$serverRequest->getMethod(),
$serverRequest->getUri()->getPath(),
$matcher->match($serverRequest),
$serverRequest,
);
}

return new RouterMatch(
$serverRequest->getMethod(),
$serverRequest->getUri()->getPath(),
$matcher->match($serverRequest),
$this->parseJson($serverRequest),
);
}

private function parseFormUrlEncoded(ServerRequestInterface $serverRequest): ServerRequestInterface
{
parse_str((string) $serverRequest->getBody(), $parsedBody);

return $serverRequest->withParsedBody($parsedBody);
}

private function parseJson(ServerRequestInterface $serverRequest): ServerRequestInterface
{
$parsedBody = json_decode(
(string) $serverRequest->getBody(),
true,
512,
JSON_THROW_ON_ERROR,
$serverRequest,
);

$error = json_last_error();
if ($error !== JSON_ERROR_NONE) {
throw new InvalidRequestException(json_last_error_msg());
}

assert(is_array($parsedBody));

return $serverRequest->withParsedBody($parsedBody);
}

/** @param array<string, string|int> $data */
Expand Down

0 comments on commit e38877f

Please # to comment.