Skip to content

Commit

Permalink
fix code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
IngeniozIT committed Feb 27, 2024
1 parent 593c4bb commit 44476e6
Show file tree
Hide file tree
Showing 21 changed files with 272 additions and 134 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace IngeniozIT\Router;
namespace IngeniozIT\Router\Exception;

use OutOfRangeException;

Expand Down
2 changes: 1 addition & 1 deletion src/InvalidRoute.php → src/Exception/InvalidRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace IngeniozIT\Router;
namespace IngeniozIT\Router\Exception;

use InvalidArgumentException;

Expand Down
11 changes: 11 additions & 0 deletions src/Exception/InvalidRouteCondition.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace IngeniozIT\Router\Exception;

use InvalidArgumentException;

final class InvalidRouteCondition extends InvalidArgumentException
{
}
11 changes: 11 additions & 0 deletions src/Exception/InvalidRouteHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace IngeniozIT\Router\Exception;

use InvalidArgumentException;

final class InvalidRouteHandler extends InvalidArgumentException
{
}
11 changes: 11 additions & 0 deletions src/Exception/InvalidRouteMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace IngeniozIT\Router\Exception;

use InvalidArgumentException;

final class InvalidRouteMiddleware extends InvalidArgumentException
{
}
11 changes: 11 additions & 0 deletions src/Exception/RouteNotFound.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

namespace IngeniozIT\Router\Exception;

use DomainException;

final class RouteNotFound extends DomainException
{
}
42 changes: 42 additions & 0 deletions src/Handler/ConditionHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace IngeniozIT\Router\Handler;

use Closure;
use IngeniozIT\Router\Exception\InvalidRouteCondition;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ServerRequestInterface;

readonly final class ConditionHandler
{
private Closure $handler;

public function __construct(
private ContainerInterface $container,
mixed $callback,
) {
$handler = is_string($callback) ? $this->container->get($callback) : $callback;

if (!is_callable($handler)) {
throw new InvalidRouteCondition('Invalid condition handler');
}

$this->handler = $handler(...);
}

/**
* @return array<string, mixed>|false
*/
public function handle(ServerRequestInterface $request): array|false
{
$result = ($this->handler)($request);

if ($result === false || is_array($result)) {
return $result;
}

throw new InvalidRouteCondition('Condition must either return an array or false.');
}
}
48 changes: 48 additions & 0 deletions src/Handler/MiddlewaresHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace IngeniozIT\Router\Handler;

use Closure;
use IngeniozIT\Router\Exception\InvalidRouteMiddleware;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

readonly final class MiddlewaresHandler
{
private Closure $handler;

public function __construct(
private ContainerInterface $container,
mixed $callback,
) {
$handler = is_string($callback) ? $this->container->get($callback) : $callback;

if (is_callable($handler)) {
$this->handler = $handler(...);
return;
}

if ($handler instanceof MiddlewareInterface) {
$this->handler = $handler->process(...);
return;
}

throw new InvalidRouteMiddleware('Middleware must be a PSR Middleware or a callable.');
}

public function handle(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$result = ($this->handler)($request, $handler);

if (!$result instanceof ResponseInterface) {
throw new InvalidRouteMiddleware('Middleware must return a PSR Response.');
}

return $result;
}
}
59 changes: 59 additions & 0 deletions src/Handler/RouteHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace IngeniozIT\Router\Handler;

use Closure;
use IngeniozIT\Router\Exception\InvalidRouteHandler;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

readonly final class RouteHandler
{
private Closure|MiddlewareInterface|RequestHandlerInterface $handler;

public function __construct(
private ContainerInterface $container,
mixed $callback,
) {
$handler = is_string($callback) ? $this->container->get($callback) : $callback;

if (
!($handler instanceof MiddlewareInterface)
&& !($handler instanceof RequestHandlerInterface)
&& !is_callable($handler)
) {
throw new InvalidRouteHandler('Route handler must be a PSR Middleware, a PSR RequestHandler or a callable.');
}

$this->handler = is_callable($handler) ? $handler(...) : $handler;
}

public function handle(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$result = $this->executeHandler($request, $handler);

if (!$result instanceof ResponseInterface) {
throw new InvalidRouteHandler('Route handler must return a PSR Response.');
}

return $result;
}

private function executeHandler(ServerRequestInterface $request, RequestHandlerInterface $handler): mixed
{
if ($this->handler instanceof RequestHandlerInterface) {
return $this->handler->handle($request);
}

if ($this->handler instanceof MiddlewareInterface) {
return $this->handler->process($request, $handler);
}

return ($this->handler)($request, $handler);
}
}
2 changes: 0 additions & 2 deletions src/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ private function extractParametersFromPath(string $path): array

/**
* @param string[][] $parameters
* @param array<string, string> $additionalPatterns
* @return array<string, string>
*/
private function extractParametersValue(array $parameters, string $path): array
Expand All @@ -182,7 +181,6 @@ private function extractParametersValue(array $parameters, string $path): array

/**
* @param string[][] $parameters
* @param array<string, string> $additionalPatterns
*/
private function buildRegex(array $parameters): string
{
Expand Down
9 changes: 5 additions & 4 deletions src/RouteGroup.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@ function (RouteGroup|Route $route) use ($with, $where, $name, $path): RouteGroup
$path,
);
}

return new Route(
$route->method,
$path . $route->path,
$route->callback,
array_merge($where, $route->where),
array_merge($with, $route->with),
name: $route->name ? $this->concatenatedName($name) . $route->name : null,
[...$where, ...$route->where],
[...$with, ...$route->with],
name: !empty($route->name) ? $this->concatenatedName($name) . $route->name : null,
);
},
$routes,
Expand All @@ -53,6 +54,6 @@ function (RouteGroup|Route $route) use ($with, $where, $name, $path): RouteGroup

private function concatenatedName(?string $name): ?string
{
return empty($name) ? $name : $name . '.';
return $name === null || $name === '' ? $name : $name . '.';
}
}
11 changes: 0 additions & 11 deletions src/RouteNotFound.php

This file was deleted.

Loading

0 comments on commit 44476e6

Please sign in to comment.