This repository was archived by the owner on Feb 24, 2025. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEndpoint.php
177 lines (157 loc) · 5.21 KB
/
Endpoint.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
<?php
declare(strict_types=1);
namespace Usox\JsonSchemaApi;
use Http\Discovery\Psr17FactoryDiscovery;
use Opis\JsonSchema\Errors\ErrorFormatter;
use Opis\JsonSchema\Validator;
use Psr\Http\Message\ResponseFactoryInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Log\LoggerInterface;
use Ramsey\Uuid\UuidFactory;
use Ramsey\Uuid\UuidFactoryInterface;
use Ramsey\Uuid\UuidInterface;
use Teapot\StatusCode\Http;
use Throwable;
use Usox\JsonSchemaApi\Contract\MethodProviderInterface;
use Usox\JsonSchemaApi\Dispatch\MethodDispatcher;
use Usox\JsonSchemaApi\Dispatch\MethodDispatcherInterface;
use Usox\JsonSchemaApi\Dispatch\MethodValidator;
use Usox\JsonSchemaApi\Dispatch\RequestValidator;
use Usox\JsonSchemaApi\Dispatch\RequestValidatorInterface;
use Usox\JsonSchemaApi\Dispatch\SchemaLoader;
use Usox\JsonSchemaApi\Exception\ApiException;
use Usox\JsonSchemaApi\Exception\InternalException;
use Usox\JsonSchemaApi\Response\ResponseBuilder;
use Usox\JsonSchemaApi\Response\ResponseBuilderInterface;
/**
* @see Endpoint::factory()
*/
final readonly class Endpoint implements
EndpointInterface
{
public function __construct(
private RequestValidatorInterface $inputValidator,
private MethodDispatcherInterface $methodRetriever,
private ResponseBuilderInterface $responseBuilder,
private UuidFactoryInterface $uuidFactory,
private StreamFactoryInterface $streamFactory,
private ResponseFactoryInterface $responseFactory,
private ?LoggerInterface $logger = null,
) {
}
/**
* Execute the api handler and build the response
*/
public function serve(
ServerRequestInterface $request,
): ResponseInterface {
$statusCode = Http::OK;
$responseData = null;
try {
// Process and build the response
$responseData = $this->responseBuilder->buildResponse(
$this->methodRetriever->dispatch(
$request,
$this->inputValidator->validate($request),
),
);
} catch (ApiException $e) {
$uuid = $this->uuidFactory->uuid4();
$this->logError($e, $uuid);
// Build an error response
$responseData = $this->responseBuilder->buildErrorResponse($e, $uuid);
$statusCode = Http::BAD_REQUEST;
} catch (InternalException $e) {
$this->logError(
$e,
$this->uuidFactory->uuid4(),
$e->getContext(),
);
$statusCode = Http::INTERNAL_SERVER_ERROR;
} catch (Throwable $e) {
$this->logError(
$e,
$this->uuidFactory->uuid4(),
);
$statusCode = Http::INTERNAL_SERVER_ERROR;
}
$response = $this->responseFactory->createResponse($statusCode);
if ($responseData !== null) {
$response = $response->withBody(
$this->streamFactory->createStream(
(string) json_encode($responseData),
),
);
}
return $response
->withHeader('Content-Type', 'application/json')
;
}
/**
* @param array<mixed, mixed> $context
*/
private function logError(
Throwable $e,
UuidInterface $uuid,
array $context = [],
): void {
$this->logger?->error(
sprintf('%s (%d)', $e->getMessage(), $e->getCode()),
[
'id' => $uuid->toString(),
'file' => $e->getFile(),
'line' => $e->getLine(),
'context' => $context,
],
);
}
/**
* Builds the endpoint.
*
* The factories may be omitted, the endpoint will try to autodetect existing PSR17 implementations
*/
public static function factory(
MethodProviderInterface $methodProvider,
?StreamFactoryInterface $streamFactory = null,
?ResponseFactoryInterface $responseFactory = null,
?LoggerInterface $logger = null,
): EndpointInterface {
$schemaValidator = new Validator();
$schemaLoader = new SchemaLoader();
if ($streamFactory === null) {
$streamFactory = Psr17FactoryDiscovery::findStreamFactory();
}
if ($responseFactory === null) {
$responseFactory = Psr17FactoryDiscovery::findResponseFactory();
}
return new self(
new RequestValidator(
$schemaLoader,
$schemaValidator,
),
new MethodDispatcher(
$schemaLoader,
new MethodValidator(
$schemaValidator,
new ErrorFormatter(),
),
$methodProvider,
$logger,
),
new ResponseBuilder(),
new UuidFactory(),
$streamFactory,
$responseFactory,
$logger,
);
}
public function handle(
ServerRequestInterface $request,
): ResponseInterface {
return $this->serve(
$request,
);
}
}