Completed
Push — code201 ( 677523...68154a )
by Akihito
08:44 queued 05:59
created

OptionsMethods   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 5
dl 0
loc 73
ccs 29
cts 29
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getInMap() 0 12 3
A __construct() 0 5 1
A __invoke() 0 14 3
A getJsonSchema() 0 13 3
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 94
    public function __construct(Reader $reader, $schemaDir = '')
45
    {
46 94
        $this->reader = $reader;
47 94
        $this->schemaDir = $schemaDir;
48 94
    }
49
50 7
    public function __invoke(ResourceObject $ro, string $requestMethod) : array
51
    {
52 7
        $method = new \ReflectionMethod($ro, 'on' . $requestMethod);
53 7
        $ins = $this->getInMap($method);
54 7
        list($doc, $paramDoc) = (new OptionsMethodDocBolck)($method);
55 7
        $paramMetas = (new OptionsMethodRequest($this->reader))($method, $paramDoc, $ins);
56 7
        $schema = $this->getJsonSchema($method);
57 7
        $request = $paramMetas ? ['request' => $paramMetas] : [];
58 7
        if (! empty($schema)) {
59 1
            return $doc + $request + ['schema' => $schema];
60
        }
61
62 6
        return $doc + $request;
63
    }
64
65 7
    private function getInMap(\ReflectionMethod $method) : array
66
    {
67 7
        $ins = [];
68 7
        $annotations = $this->reader->getMethodAnnotations($method);
69 7
        foreach ($annotations as $annotation) {
70 5
            if ($annotation instanceof AbstractWebContextParam) {
71 5
                $ins[$annotation->param] = self::WEB_CONTEXT_NAME[get_class($annotation)];
72
            }
73
        }
74
75 7
        return $ins;
76
    }
77
78 7
    private function getJsonSchema(\ReflectionMethod $method) : array
79
    {
80 7
        $schema = $this->reader->getMethodAnnotation($method, JsonSchema::class);
81 7
        if (! $schema instanceof JsonSchema) {
82 5
            return [];
83
        }
84 2
        $schemaFile = $this->schemaDir . '/' . $schema->schema;
85 2
        if (! file_exists($schemaFile)) {
86 1
            return [];
87
        }
88
89 1
        return (array) json_decode(file_get_contents($schemaFile));
90
    }
91
}
92