1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace BEAR\Resource\Interceptor; |
6
|
|
|
|
7
|
|
|
use BEAR\Resource\Annotation\JsonSchema; |
8
|
|
|
use BEAR\Resource\Code; |
9
|
|
|
use BEAR\Resource\Exception\JsonSchemaException; |
10
|
|
|
use BEAR\Resource\Exception\JsonSchemaNotFoundException; |
11
|
|
|
use BEAR\Resource\JsonSchemaExceptionHandlerInterface; |
12
|
|
|
use BEAR\Resource\ResourceObject; |
13
|
|
|
use function is_string; |
14
|
|
|
use JsonSchema\Constraints\Constraint; |
15
|
|
|
use JsonSchema\Validator; |
16
|
|
|
use Ray\Aop\MethodInterceptor; |
17
|
|
|
use Ray\Aop\MethodInvocation; |
18
|
|
|
use Ray\Aop\ReflectionMethod; |
19
|
|
|
use Ray\Aop\WeavedInterface; |
20
|
|
|
use Ray\Di\Di\Named; |
21
|
|
|
|
22
|
|
|
final class JsonSchemaInterceptor implements MethodInterceptor |
23
|
|
|
{ |
24
|
1 |
|
/** |
25
|
|
|
* @var string |
26
|
1 |
|
*/ |
27
|
|
|
private $schemaDir; |
28
|
1 |
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
1 |
|
*/ |
32
|
1 |
|
private $validateDir; |
33
|
1 |
|
|
34
|
1 |
|
/** |
35
|
1 |
|
* @var null|string |
36
|
1 |
|
*/ |
37
|
1 |
|
private $schemaHost; |
38
|
1 |
|
|
39
|
1 |
|
/** |
40
|
|
|
* @var JsonSchemaExceptionHandlerInterface |
41
|
|
|
*/ |
42
|
|
|
private $handler; |
43
|
1 |
|
|
44
|
1 |
|
/** |
45
|
1 |
|
* @Named("schemaDir=json_schema_dir,validateDir=json_validate_dir,schemaHost=json_schema_host") |
46
|
1 |
|
*/ |
47
|
|
|
public function __construct(string $schemaDir, string $validateDir, JsonSchemaExceptionHandlerInterface $handler, string $schemaHost = null) |
48
|
1 |
|
{ |
49
|
|
|
$this->schemaDir = $schemaDir; |
50
|
|
|
$this->validateDir = $validateDir; |
51
|
|
|
$this->schemaHost = $schemaHost; |
52
|
|
|
$this->handler = $handler; |
53
|
|
|
} |
54
|
1 |
|
|
55
|
|
|
/** |
56
|
1 |
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
|
|
public function invoke(MethodInvocation $invocation) |
59
|
|
|
{ |
60
|
1 |
|
/** @var ReflectionMethod $method */ |
61
|
|
|
$method = $invocation->getMethod(); |
62
|
|
|
/** @var JsonSchema $jsonSchema */ |
63
|
|
|
$jsonSchema = $method->getAnnotation(JsonSchema::class); |
64
|
|
|
if ($jsonSchema->params) { |
65
|
|
|
$arguments = $this->getNamedArguments($invocation); |
66
|
|
|
$this->validateRequest($jsonSchema, $arguments); |
67
|
|
|
} |
68
|
|
|
/** @var ResourceObject $ro */ |
69
|
|
|
$ro = $invocation->proceed(); |
70
|
|
|
if ($ro->code === 200 || $ro->code == 201) { |
71
|
|
|
$this->validateResponse($ro, $jsonSchema); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return $ro; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
private function validateRequest(JsonSchema $jsonSchema, array $arguments) |
78
|
|
|
{ |
79
|
|
|
$schemaFile = $this->validateDir . '/' . $jsonSchema->params; |
80
|
|
|
$this->validateFileExists($schemaFile); |
81
|
|
|
$this->validate($arguments, $schemaFile); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
private function validateResponse(ResourceObject $ro, JsonSchema $jsonSchema) |
85
|
|
|
{ |
86
|
|
|
$schemaFile = $this->getSchemaFile($jsonSchema, $ro); |
87
|
|
|
try { |
88
|
|
|
$this->validateRo($ro, $schemaFile); |
89
|
|
|
if (is_string($this->schemaHost)) { |
90
|
|
|
$ro->headers['Link'] = sprintf('<%s%s>; rel="describedby"', $this->schemaHost, $jsonSchema->schema); |
91
|
|
|
} |
92
|
|
|
} catch (JsonSchemaException $e) { |
93
|
|
|
$this->handler->handle($ro, $e, $schemaFile); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
private function validateRo(ResourceObject $ro, string $schemaFile) |
98
|
|
|
{ |
99
|
|
|
$validator = new Validator; |
|
|
|
|
100
|
|
|
$json = json_decode((string) $ro); |
101
|
|
|
$this->validate($json, $schemaFile); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
private function validate($scanObject, $schemaFile) |
105
|
|
|
{ |
106
|
|
|
$validator = new Validator; |
107
|
|
|
$schema = (object) ['$ref' => 'file://' . $schemaFile]; |
108
|
|
|
$validator->validate($scanObject, $schema, Constraint::CHECK_MODE_TYPE_CAST); |
109
|
|
|
$isValid = $validator->isValid(); |
110
|
|
|
if ($isValid) { |
111
|
|
|
return; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
throw $this->throwJsonSchemaException($validator); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
private function throwJsonSchemaException(Validator $validator) : JsonSchemaException |
118
|
|
|
{ |
119
|
|
|
$errors = $validator->getErrors(); |
120
|
|
|
$msg = ''; |
121
|
|
|
foreach ($errors as $error) { |
122
|
|
|
$msg .= sprintf('[%s] %s; ', $error['property'], $error['message']); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
return new JsonSchemaException($msg, Code::ERROR); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
private function getSchemaFile(JsonSchema $jsonSchema, ResourceObject $ro) : string |
129
|
|
|
{ |
130
|
|
|
if (! $jsonSchema->schema) { |
131
|
|
|
// for BC only |
132
|
|
|
$ref = new \ReflectionClass($ro); |
133
|
|
|
if (! $ref instanceof \ReflectionClass) { |
134
|
|
|
throw new \ReflectionException(get_class($ro)); // @codeCoverageIgnore |
135
|
|
|
} |
136
|
|
|
$roFileName = $ro instanceof WeavedInterface ? $roFileName = $ref->getParentClass()->getFileName() : $ref->getFileName(); |
137
|
|
|
$bcFile = str_replace('.php', '.json', $roFileName); |
138
|
|
|
if (file_exists($bcFile)) { |
139
|
|
|
return $bcFile; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
$schemaFile = $this->schemaDir . '/' . $jsonSchema->schema; |
143
|
|
|
$this->validateFileExists($schemaFile); |
144
|
|
|
|
145
|
|
|
return $schemaFile; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
private function validateFileExists(string $schemaFile) |
149
|
|
|
{ |
150
|
|
|
if (! file_exists($schemaFile) || is_dir($schemaFile)) { |
151
|
|
|
throw new JsonSchemaNotFoundException($schemaFile); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
private function getNamedArguments(MethodInvocation $invocation) |
156
|
|
|
{ |
157
|
|
|
$parameters = $invocation->getMethod()->getParameters(); |
158
|
|
|
$values = $invocation->getArguments(); |
159
|
|
|
$arguments = []; |
160
|
|
|
foreach ($parameters as $index => $parameter) { |
161
|
|
|
$arguments[$parameter->name] = $values[$index] ?? $parameter->getDefaultValue(); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return $arguments; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.