Completed
Branch 1.x (2c7718)
by Akihito
01:38
created

Meta::getParams()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.054

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 9
cts 11
cp 0.8182
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 11
nc 3
nop 2
crap 3.054
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
final class Meta
10
{
11
    const EXTRAS_VENDOR = 'vendor';
12
13
    const EXTRAS_PACKAGE = 'package';
14
15
    /**
16
     * @var string
17
     */
18
    public $uri;
19
20
    /**
21
     * @var Options
22
     */
23
    public $options;
24
25
    /**
26
     * @var array
27
     */
28
    public $extras = [];
29
30
    /**
31
     * @param string $class
32
     */
33 1
    public function __construct($class)
34
    {
35 1
        $this->uri = $this->getUri($class);
36 1
        $this->options = $this->getOptions($class);
37 1
    }
38
39 1
    private function getUri(string $class) : string
40
    {
41 1
        $classPath = explode('\\', $class);
42
        // $class
43 1
        $this->extras[self::EXTRAS_VENDOR] = array_shift($classPath);
44 1
        $this->extras[self::EXTRAS_PACKAGE] = array_shift($classPath);
45 1
        array_shift($classPath); // "/Resource/"
46 1
        $scheme = array_shift($classPath);
47 1
        $uri = strtolower("{$scheme}://self/" . implode('/', $classPath));
48
49 1
        return $uri;
50
    }
51
52
    /**
53
     * Return available resource request method
54
     */
55 1
    private function getOptions(string $class) : Options
56
    {
57 1
        $ref = new \ReflectionClass($class);
58 1
        $allows = $this->getAllows($ref->getMethods());
59 1
        $params = [];
60 1
        foreach ($allows as $method) {
61 1
            $params[] = $this->getParams($class, $method);
62
        }
63 1
        $options = new Options($allows, $params);
64
65 1
        return $options;
66
    }
67
68
    /**
69
     * @param \ReflectionMethod[] $methods
70
     */
71 1
    private function getAllows(array $methods) : array
72
    {
73 1
        $allows = [];
74 1
        foreach ($methods as $method) {
75 1
            $isRequestMethod = strpos($method->name, 'on') === 0 && strpos($method->name, 'onLink') !== 0;
76 1
            if ($isRequestMethod) {
77 1
                $allows[] = strtolower(substr($method->name, 2));
78
            }
79
        }
80
81 1
        return $allows;
82
    }
83
84 1
    private function getParams(string $class, string $method) : Params
85
    {
86 1
        $refMethod = new \ReflectionMethod($class, 'on' . $method);
87 1
        $parameters = $refMethod->getParameters();
88 1
        $optionalParams = $requiredParams = [];
89 1
        foreach ($parameters as $parameter) {
90 1
            $name = $parameter->name;
91 1
            if ($parameter->isOptional()) {
92
                $optionalParams[] = $name;
93
                continue;
94
            }
95 1
            $requiredParams[] = $name;
96
        }
97
98 1
        return new Params($method, $requiredParams, $optionalParams);
99
    }
100
}
101