Completed
Push — code201 ( 677523...68154a )
by Akihito
05:57
created

OptionsMethods::paramType()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 9
rs 9.6666
ccs 5
cts 5
cp 1
cc 2
eloc 5
nc 2
nop 2
crap 2
1
<?php
2
/**
3
 * This file is part of the BEAR.Resource package.
4
 *
5
 * @license http://opensource.org/licenses/MIT MIT
6
 */
7
namespace BEAR\Resource;
8
9
use BEAR\Resource\Annotation\JsonSchema;
10
use Doctrine\Common\Annotations\Reader;
11
use Ray\Di\Di\Named;
12
use Ray\WebContextParam\Annotation\AbstractWebContextParam;
13
use Ray\WebContextParam\Annotation\CookieParam;
14
use Ray\WebContextParam\Annotation\EnvParam;
15
use Ray\WebContextParam\Annotation\FormParam;
16
use Ray\WebContextParam\Annotation\QueryParam;
17
use Ray\WebContextParam\Annotation\ServerParam;
18
19
final class OptionsMethods
20
{
21
    /**
22
     * Constants for annotation name and "in" name
23
     *
24
     * @var array
25
     */
26
    const WEB_CONTEXT_NAME = [
27
        CookieParam::class => 'cookie',
28
        EnvParam::class => 'env',
29
        FormParam::class => 'formData',
30
        QueryParam::class => 'query',
31
        ServerParam::class => 'server'
32
    ];
33
34
    private $reader;
35
36
    /**
37
     * @var string
38
     */
39
    private $schemaDir;
40
41
    /**
42
     * @Named("schemaDir=json_schema_dir")
43
     */
44
    public function __construct(Reader $reader, $schemaDir = '')
45
    {
46 89
        $this->reader = $reader;
47
        $this->schemaDir = $schemaDir;
48 89
    }
49 89
50 89
    public function __invoke(ResourceObject $ro, string $requestMethod) : array
51
    {
52 6
        $method = new \ReflectionMethod($ro, 'on' . $requestMethod);
53
        $ins = $this->getInMap($method);
54 6
        list($doc, $paramDoc) = (new OptionsMethodDocBolck)($method);
55 6
        $paramMetas = (new OptionsMethodRequest($this->reader))($method, $paramDoc, $ins);
56 6
        $schema = $this->getJsonSchema($method);
57 6
        $request = $paramMetas ? ['request' => $paramMetas] : [];
58 6
        if (! empty($schema)) {
59 6
            return $doc + $request + ['schema' => $schema];
60
        }
61 6
62 6
        return $doc + $request;
63 6
    }
64 6
65 6
    private function getInMap(\ReflectionMethod $method) : array
66
    {
67 6
        $ins = [];
68 6
        $annotations = $this->reader->getMethodAnnotations($method);
69
        foreach ($annotations as $annotation) {
70 6
            if ($annotation instanceof AbstractWebContextParam) {
71 6
                $ins[$annotation->param] = self::WEB_CONTEXT_NAME[get_class($annotation)];
72 6
            }
73 6
        }
74 1
75
        return $ins;
76
    }
77 5
78
    private function getJsonSchema(\ReflectionMethod $method) : array
79
    {
80 6
        $schema = $this->reader->getMethodAnnotation($method, JsonSchema::class);
81
        if (! $schema instanceof JsonSchema) {
82 6
            return [];
83 6
        }
84 6
        $schemaFile = $this->schemaDir . '/' . $schema->schema;
85 4
        if (! file_exists($schemaFile)) {
86 4
            return [];
87
        }
88
89
        return (array) json_decode(file_get_contents($schemaFile));
90 6
    }
91
}
92