-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRestWrapper.php
More file actions
177 lines (156 loc) Β· 5.09 KB
/
RestWrapper.php
File metadata and controls
177 lines (156 loc) Β· 5.09 KB
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
/**
* Copyright (c) Dimitri BOUTEILLE (https://github.com/dimitriBouteille)
* See LICENSE.txt for license details.
*
* Author: Dimitri BOUTEILLE <bonjour@dimitri-bouteille.fr>
*/
namespace Dbout\WpRestApi\Wrappers;
use Dbout\WpRestApi\Exceptions\RouteException;
use Dbout\WpRestApi\RouteAction;
class RestWrapper
{
private const DEFAULT_EXCEPTION_CODE = 'route-exception';
private const DEFAULT_EXCEPTION_HTTP_CODE = 500;
/**
* @param RouteAction $action
* @param bool $debug
*/
public function __construct(
protected RouteAction $action,
protected bool $debug = false,
) {
}
/**
* @param \WP_REST_Request $request
* @return \WP_REST_Response
*/
public function execute(\WP_REST_Request $request): \WP_REST_Response
{
try {
$classRef = new \ReflectionClass($this->action->className);
$method = $classRef->getMethod($this->action->methodName);
$dependencies = $this->collectDependencies($method, $request);
$response = $method->invoke($classRef->newInstance(), ...$dependencies);
} catch (\Exception $exception) {
return $this->onError($exception);
}
if (is_wp_error($response)) {
return $this->parseErrorToRestResponse(
$response,
self::DEFAULT_EXCEPTION_HTTP_CODE
);
}
return $response;
}
/**
* @param \Exception $exception
* @return \WP_REST_Response
*/
protected function onError(\Exception $exception): \WP_REST_Response
{
$rootException = $exception;
if (!$exception instanceof RouteException) {
$exception = new RouteException(
message: 'Something went wrong. Please try again.',
errorCode: 'fatal-error',
httpStatusCode: self::DEFAULT_EXCEPTION_HTTP_CODE,
);
}
if ($this->debug === true) {
$exception = new RouteException(
message: $rootException->getMessage(),
errorCode: $exception->getErrorCode(),
httpStatusCode: $exception->getHttpStatusCode(),
additionalData: array_merge($exception->getAdditionalData(), [
'exception' => $rootException->getTraceAsString(),
]),
previous: $rootException,
);
}
return $this->parseErrorToRestResponse(
$this->buildResponseError($exception),
$exception->getHttpStatusCode()
);
}
/**
* @param \ReflectionMethod $method
* @param \WP_REST_Request $request
* @return array<int, mixed>
*/
protected function collectDependencies(\ReflectionMethod $method, \WP_REST_Request $request): array
{
$dependencies = [];
foreach ($method->getParameters() as $parameter) {
$type = $parameter->getType();
if (!$type instanceof \ReflectionNamedType) {
continue;
}
if ($type->getName() === get_class($request)) {
$dependencies[$parameter->getPosition()] = $request;
continue;
}
if (!$request->has_param($parameter->getName())) {
continue;
}
$value = $request->get_param($parameter->getName());
$value = $this->castRequestArgument($type, $value);
$dependencies[$parameter->getPosition()] = $value;
}
return $dependencies;
}
/**
* @param \ReflectionNamedType $type
* @param mixed $value
* @return mixed
*/
protected function castRequestArgument(\ReflectionNamedType $type, mixed $value): mixed
{
if ($value === null) {
return null;
}
return match ($type->getName()) {
'int' => (int)$value,
'string' => (string)$value,
default => $value,
};
}
/**
* @param RouteException $exception
* @return \WP_Error
*/
protected function buildResponseError(
RouteException $exception,
): \WP_Error {
return new \WP_Error(
$exception->getErrorCode() ?? self::DEFAULT_EXCEPTION_CODE,
$exception->getMessage(),
$exception->getAdditionalData()
);
}
/**
* @param \WP_Error $error
* @param int $httpCode
* @return \WP_REST_Response
*/
protected function parseErrorToRestResponse(\WP_Error $error, int $httpCode): \WP_REST_Response
{
$errors = [];
foreach ((array) $error->errors as $code => $messages) {
foreach ((array) $messages as $message) {
$errors[] = [
'code' => $code,
'message' => $message,
'data' => $error->get_error_data($code),
];
}
}
$data = array_shift($errors);
if ($errors !== []) {
$data['additional_errors'] = $errors;
}
return new \WP_REST_Response([
'error' => $data,
], $httpCode);
}
}