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

NamedParameter::getAssistedNames()   A

Complexity

Conditions 4
Paths 5

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 6
cts 6
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 8
nc 5
nop 1
crap 4
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\ResourceParam;
10
use Doctrine\Common\Annotations\Reader;
11
use Doctrine\Common\Cache\Cache;
12
use Ray\Di\Di\Assisted;
13
use Ray\Di\InjectorInterface;
14
use Ray\WebContextParam\Annotation\AbstractWebContextParam;
15
16
final class NamedParameter implements NamedParameterInterface
17
{
18
    /**
19
     * @var Cache
20
     */
21
    private $cache;
22
23
    /**
24
     * @var Reader
25
     */
26
    private $reader;
27
28
    /**
29
     * @var InjectorInterface
30
     */
31
    private $injector;
32
33
    /**
34
     * @var array
35
     */
36
    private $globals;
37
38 97
    public function __construct(Cache $cache, Reader $reader, InjectorInterface $injector, array $globals = [])
39
    {
40 97
        $this->cache = $cache;
41 97
        $this->reader = $reader;
42 97
        $this->injector = $injector;
43 97
        $this->globals = $globals;
44 97
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 62
    public function getParameters(array $callable, array $query)
50
    {
51 62
        $cacheId = __CLASS__ . get_class($callable[0]) . $callable[1];
52 62
        $names = $this->cache->fetch($cacheId);
53 62
        if (! $names) {
54 60
            $names = $this->getNamedParamMetas($callable);
55 60
            $this->cache->save($cacheId, $names);
56
        }
57 62
        $parameters = $this->evaluateParams($query, $names);
58
59 55
        return $parameters;
60
    }
61
62
    /**
63
     * Return evaluated parameters
64
     *
65
     * @param array            $query caller value
66
     * @param ParamInterface[] $names Param object[] ['varName' => ParamInterface]
67
     */
68 62
    private function evaluateParams(array $query, array $names) : array
69
    {
70 62
        $parameters = [];
71 62
        foreach ($names as $varName => $param) {
72
            /* @var $param ParamInterface */
73 54
            $parameters[] = $param($varName, $query, $this->injector);
74
        }
75
76 55
        return $parameters;
77
    }
78
79
    /**
80
     * Return named parameter metas
81
     */
82 60
    private function getNamedParamMetas(callable $callable) : array
83
    {
84 60
        $method = new \ReflectionMethod($callable[0], $callable[1]);
85 60
        $parameters = $method->getParameters();
86 60
        $annotations = $this->reader->getMethodAnnotations($method);
87 60
        $assistedNames = $this->getAssistedNames($annotations);
88 60
        $webContext = $this->getWebContext($annotations);
89 51
        $namedParamMetas = $this->addNamedParams($parameters, $assistedNames, $webContext);
90 4
91 4
        return $namedParamMetas;
92
    }
93 50
94 4
    private function getAssistedNames(array $annotations) : array
95 4
    {
96 4
        $names = [];
97
        foreach ($annotations as $annotation) {
98 48
            if ($annotation instanceof ResourceParam) {
99
                $names[$annotation->param] = new AssistedResourceParam($annotation);
100
            }
101 60
            if ($annotation instanceof Assisted) {
102
                $names = $this->setAssistedAnnotation($names, $annotation);
103
            }
104
        }
105
106
        return $names;
107 60
    }
108
109 60
    private function getWebContext(array $annotations) : array
110 60
    {
111 60
        $webcontext = [];
112 28
        foreach ($annotations as $annotation) {
113 3
            if ($annotation instanceof AbstractWebContextParam) {
114
                $webcontext[$annotation->param] = $annotation;
115 28
            }
116 1
        }
117
118 28
        return $webcontext;
119 28
    }
120
121
    private function setAssistedAnnotation(array $names, Assisted $assisted) : array
122
    {
123 60
        /* @var $annotation Assisted */
124
        foreach ($assisted->values as $assistedParam) {
125
            $names[$assistedParam] = new AssistedParam;
126
        }
127
128
        return $names;
129 1
    }
130
131
    /**
132 1
     * @param \ReflectionParameter[] $parameters
133 1
     * @param array                  $assistedNames
134
     * @param array                  $webcontext
135
     *
136 1
     * @return ParamInterface[]
137
     */
138
    private function addNamedParams(array $parameters, array $assistedNames, array $webcontext) : array
139
    {
140
        $names = [];
141
        foreach ($parameters as $parameter) {
142
            if (isset($assistedNames[$parameter->name])) {
143
                $names[$parameter->name] = $assistedNames[$parameter->name];
144
                continue;
145
            }
146
            if (isset($webcontext[$parameter->name])) {
147
                $default = $this->getDefault($parameter);
148
                $names[$parameter->name] = new AssistedWebContextParam($webcontext[$parameter->name], $default);
149
                continue;
150
            }
151
            $names[$parameter->name] = $this->getParam($parameter);
152
        }
153
154
        return $names;
155
    }
156
157
    private function getDefault(\ReflectionParameter $parameter) : ParamInterface
158
    {
159
        return $parameter->isDefaultValueAvailable() === true ? new DefaultParam($parameter->getDefaultValue()) : new NoDefaultParam();
160
    }
161
162
    private function getParam(\ReflectionParameter $parameter) : ParamInterface
163
    {
164
        return $parameter->isDefaultValueAvailable() === true ? new OptionalParam($parameter->getDefaultValue()) : new RequiredParam;
165
    }
166
}
167