| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Usox\JsonSchemaApi\Dispatch; |
||
| 6 | |||
| 7 | use Psr\Http\Message\ServerRequestInterface; |
||
| 8 | use Psr\Log\LoggerInterface; |
||
| 9 | use stdClass; |
||
| 10 | use Teapot\StatusCode\Http; |
||
| 11 | use Usox\JsonSchemaApi\Contract\ApiMethodInterface; |
||
| 12 | use Usox\JsonSchemaApi\Contract\MethodProviderInterface; |
||
| 13 | use Usox\JsonSchemaApi\Dispatch\Exception\SchemaInvalidException; |
||
| 14 | use Usox\JsonSchemaApi\Dispatch\Exception\SchemaNotFoundException; |
||
| 15 | use Usox\JsonSchemaApi\Dispatch\Exception\SchemaNotLoadableException; |
||
| 16 | use Usox\JsonSchemaApi\Exception\ApiMethodException; |
||
| 17 | use Usox\JsonSchemaApi\Exception\MethodNotFoundException; |
||
| 18 | use Usox\JsonSchemaApi\Exception\RequestMalformedException; |
||
| 19 | use Usox\JsonSchemaApi\Exception\ResponseMalformedException; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Handles the life cycle of a method call |
||
| 23 | * - load method handler |
||
| 24 | * - validate input |
||
| 25 | * - execute handler |
||
| 26 | * - validate output |
||
| 27 | */ |
||
| 28 | final readonly class MethodDispatcher implements MethodDispatcherInterface |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 29 | { |
||
| 30 | 4 | public function __construct( |
|
| 31 | private SchemaLoaderInterface $schemaLoader, |
||
| 32 | private MethodValidatorInterface $methodValidator, |
||
| 33 | private MethodProviderInterface $methodProvider, |
||
| 34 | private ?LoggerInterface $logger, |
||
| 35 | ) { |
||
| 36 | 4 | } |
|
| 37 | |||
| 38 | /** |
||
| 39 | * @return array<mixed, mixed> |
||
| 40 | * |
||
| 41 | * @throws MethodNotFoundException |
||
| 42 | * @throws ApiMethodException |
||
| 43 | * @throws RequestMalformedException |
||
| 44 | * @throws ResponseMalformedException |
||
| 45 | * @throws SchemaInvalidException |
||
| 46 | * @throws SchemaNotFoundException |
||
| 47 | * @throws SchemaNotLoadableException |
||
| 48 | */ |
||
| 49 | 2 | public function dispatch( |
|
| 50 | ServerRequestInterface $request, |
||
| 51 | stdClass $input, |
||
| 52 | ): array { |
||
| 53 | // Get the method from the request and perform lookup |
||
| 54 | 2 | $methodName = $input->method; |
|
| 55 | |||
| 56 | 2 | $this->logger?->debug( |
|
| 57 | 2 | 'Api method call', |
|
| 58 | 2 | [ |
|
| 59 | 2 | 'method' => $methodName, |
|
| 60 | 2 | 'input' => $input->parameter, |
|
| 61 | 2 | ], |
|
| 62 | 2 | ); |
|
| 63 | |||
| 64 | 2 | $handler = $this->methodProvider->lookup($methodName); |
|
| 65 | 2 | if (!$handler instanceof ApiMethodInterface) { |
|
| 66 | 1 | throw new MethodNotFoundException( |
|
| 67 | 1 | 'Method not found', |
|
| 68 | 1 | Http::BAD_REQUEST, |
|
| 69 | 1 | ); |
|
| 70 | } |
||
| 71 | |||
| 72 | 1 | $schemaContent = $this->schemaLoader->load($handler->getSchemaFile()); |
|
| 73 | |||
| 74 | 1 | $this->methodValidator->validateInput($schemaContent, $input); |
|
| 75 | |||
| 76 | 1 | $this->logger?->info( |
|
| 77 | 1 | 'Api method call', |
|
| 78 | 1 | [ |
|
| 79 | 1 | 'method' => $methodName, |
|
| 80 | 1 | ], |
|
| 81 | 1 | ); |
|
| 82 | |||
| 83 | 1 | $response = $handler->handle($request, $input->parameter); |
|
| 84 | |||
| 85 | 1 | $this->methodValidator->validateOutput( |
|
| 86 | 1 | $schemaContent, |
|
| 87 | 1 | $response, |
|
| 88 | 1 | ); |
|
| 89 | |||
| 90 | 1 | return $response; |
|
| 91 | } |
||
| 92 | } |
||
| 93 |