Completed
Push — fake_json ( 7e34ad )
by Akihito
08:12
created

JsonSchemaInterceptor::fakeResponse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
cc 1
nc 1
nop 1
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\JsonSchemaErrorException;
10
use BEAR\Resource\Exception\JsonSchemaException;
11
use BEAR\Resource\Exception\JsonSchemaNotFoundException;
12
use BEAR\Resource\ResourceObject;
13
use function file_get_contents;
14
use function is_string;
15
use function json_decode;
16
use JsonSchema\Constraints\Constraint;
17
use JsonSchema\Validator;
18
use JSONSchemaFaker\Faker;
19
use Ray\Aop\MethodInterceptor;
20
use Ray\Aop\MethodInvocation;
21
use Ray\Aop\ReflectionMethod;
22
use Ray\Aop\WeavedInterface;
23
use Ray\Di\Di\Named;
24
25
final class JsonSchemaInterceptor implements MethodInterceptor
26
{
27
    const X_FAKE_JSON = 'X-Fake-JSON';
28
29
    /**
30
     * @var string
31
     */
32
    private $schemaDir;
33
34
    /**
35
     * @var string
36
     */
37
    private $validateDir;
38
39
    /**
40
     * @var null|string
41
     */
42
    private $schemaHost;
43
44
    /**
45
     * @var bool
46
     */
47
    private $enableFakeJson;
48
49
    /**
50
     * @Named("schemaDir=json_schema_dir,validateDir=json_validate_dir,schemaHost=json_schema_host,enableFakeJson=enable_fake_json")
51
     */
52
    public function __construct(string $schemaDir, string $validateDir, string $schemaHost = null, bool $enableFakeJson = false)
53
    {
54
        $this->schemaDir = $schemaDir;
55
        $this->validateDir = $validateDir;
56
        $this->schemaHost = $schemaHost;
57
        $this->enableFakeJson = $enableFakeJson;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function invoke(MethodInvocation $invocation)
64
    {
65
        /** @var ReflectionMethod $method */
66
        $method = $invocation->getMethod();
67
        /** @var JsonSchema $jsonSchema */
68
        $jsonSchema = $method->getAnnotation(JsonSchema::class);
69
        if ($jsonSchema->params) {
70
            $arguments = $this->getNamedArguments($invocation);
71
            $this->validateRequest($jsonSchema, $arguments);
72
        }
73
        /** @var ResourceObject $ro */
74
        $ro = $invocation->proceed();
75
        if (is_string($this->schemaHost)) {
76
            $ro->headers['Link'] = sprintf('<%s%s>; rel="describedby"', $this->schemaHost, $jsonSchema->schema);
77
        }
78
        if ($ro->code !== 200 && $ro->code !== 201) {
79
            return $ro;
80
        }
81
        try {
82
            $this->validateResponse($jsonSchema, $ro);
83
        } catch (JsonSchemaException $e) {
84
            if ($this->enableFakeJson) {
85
                $ro->headers[self::X_FAKE_JSON] = $jsonSchema->schema;
86
                $ro->body = $this->fakeResponse($jsonSchema);
87
88
                return $ro;
89
            }
90
91
            throw $e;
92
        }
93
94
        return $ro;
95
    }
96
97
    private function fakeResponse(JsonSchema $jsonSchema) : array
98
    {
99
        $schemaFile = $this->schemaDir . '/' . $jsonSchema->schema;
100
        $this->validateFileExists($schemaFile);
101
        $schema = json_decode(file_get_contents($schemaFile));
102
        $fakeJson = (new Faker($this->schemaDir))->generate($schema);
103
104
        return $this->deepArray($fakeJson);
105
    }
106
107
    private function deepArray($values) : array
108
    {
109
        $result = [];
110
        foreach ($values as $key => $value) {
111
            $result[$key] = is_object($value) ? $this->deepArray((array) $value) : $result[$key] = $value;
112
        }
113
114
        return $result;
115
    }
116
117
    private function validateRequest(JsonSchema $jsonSchema, array $arguments)
118
    {
119
        $schemaFile = $this->validateDir . '/' . $jsonSchema->params;
120
        $this->validateFileExists($schemaFile);
121
        $this->validate($arguments, $schemaFile);
122
    }
123
124
    private function validateResponse(JsonSchema $jsonSchema, ResourceObject $ro)
125
    {
126
        $schemaFile = $this->getSchemaFile($jsonSchema, $ro);
127
        $body = isset($ro->body[$jsonSchema->key]) ? $ro->body[$jsonSchema->key] : $ro->body;
128
        $this->validate($body, $schemaFile);
129
    }
130
131
    private function validate($scanObject, $schemaFile)
132
    {
133
        $validator = new Validator;
134
        $schema = (object) ['$ref' => 'file://' . $schemaFile];
135
        $validator->validate($scanObject, $schema, Constraint::CHECK_MODE_TYPE_CAST);
136
        $isValid = $validator->isValid();
137
        if ($isValid) {
138
            return;
139
        }
140
        $e = null;
141
        foreach ($validator->getErrors() as $error) {
142
            $msg = sprintf('[%s] %s', $error['property'], $error['message']);
143
            $e = $e ? new JsonSchemaErrorException($msg, 0, $e) : new JsonSchemaErrorException($msg);
144
        }
145
146
        throw new JsonSchemaException($schemaFile, Code::ERROR, $e);
147
    }
148
149
    private function getSchemaFile(JsonSchema $jsonSchema, ResourceObject $ro) : string
150
    {
151
        if (! $jsonSchema->schema) {
152
            // for BC only
153
            $ref = new \ReflectionClass($ro);
154
            if (! $ref instanceof \ReflectionClass) {
155
                throw new \ReflectionException(get_class($ro)); // @codeCoverageIgnore
156
            }
157
            $roFileName = $ro instanceof WeavedInterface ? $roFileName = $ref->getParentClass()->getFileName() : $ref->getFileName();
158
            $bcFile = str_replace('.php', '.json', $roFileName);
159
            if (file_exists($bcFile)) {
160
                return $bcFile;
161
            }
162
        }
163
        $schemaFile = $this->schemaDir . '/' . $jsonSchema->schema;
164
        $this->validateFileExists($schemaFile);
165
166
        return $schemaFile;
167
    }
168
169
    private function validateFileExists(string $schemaFile)
170
    {
171
        if (! file_exists($schemaFile) || is_dir($schemaFile)) {
172
            throw new JsonSchemaNotFoundException($schemaFile);
173
        }
174
    }
175
176
    private function getNamedArguments(MethodInvocation $invocation)
177
    {
178
        $parameters = $invocation->getMethod()->getParameters();
179
        $values = $invocation->getArguments();
180
        $arguments = [];
181
        foreach ($parameters as $index => $parameter) {
182
            $arguments[$parameter->name] = $values[$index] ?? $parameter->getDefaultValue();
183
        }
184
185
        return $arguments;
186
    }
187
}
188