Passed
Push — master ( 04bb0b...31510a )
by Daniel
11:10
created

MethodDispatcher::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 0
nc 1
nop 4
dl 0
loc 6
ccs 1
cts 1
cp 1
crap 1
rs 10
c 1
b 0
f 0
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;
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 class MethodDispatcher implements MethodDispatcherInterface
29
{
30 4
    public function __construct(
31
        private SchemaLoaderInterface $schemaLoader,
32
        private MethodValidatorInterface $methodValidator,
33
        private MethodProviderInterface $methodProvider,
34
        private ?LoggerInterface $logger
35
    ) {
36
    }
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
            'Api method call',
58
            [
59 2
                'method' => $methodName,
60 2
                'input' => $input->parameter
61
            ]
62
        );
63
64 2
        $handler = $this->methodProvider->lookup($methodName);
65 2
        if (!$handler instanceof ApiMethodInterface) {
66 1
            throw new MethodNotFoundException(
67
                'Method not found',
68
                StatusCode::BAD_REQUEST
69
            );
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
            'Api method call',
78
            [
79 1
                'method' => $methodName,
80
            ]
81
        );
82
83 1
        $response = $handler->handle($request, $input->parameter);
84
85 1
        $this->methodValidator->validateOutput(
86
            $schemaContent,
87
            $response
88
        );
89
90 1
        return $response;
91
    }
92
}
93