antidot-framework/antidot-framework

View on GitHub
src/Middleware/RequestHandlerFactory.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php

declare(strict_types=1);

namespace Antidot\Framework\Middleware;

use Closure;
use Psr\Container\ContainerInterface;
use Psr\Http\Server\RequestHandlerInterface;

use function is_string;

final class RequestHandlerFactory
{
    public function __construct(
        private readonly ContainerInterface $container
    ) {
    }

    public function create(RequestHandlerInterface|Closure|string $handler): RequestHandlerInterface
    {
        if ($handler instanceof RequestHandlerInterface) {
            return $handler;
        }

        if (is_string($handler)) {
            return new LazyLoadingRequestHandler($this->container, $handler);
        }

        return new CallableRequestHandler($handler);
    }
}