Completed
Push — master ( 165a9d...2cb5e6 )
by John
01:50
created

buildVersionedEndpointNamespace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
namespace LunixREST\Endpoint;
3
4
use LunixREST\Endpoint\Exceptions\UnknownEndpointException;
5
6
class NamespaceEndpointFactory implements EndpointFactory {
7
8
    protected $endpointNamespace;
9
10
    /**
11
     * NamespaceEndpointFactory constructor.
12
     * @param string $endpointNamespace
13
     */
14
    public function __construct(string $endpointNamespace) {
15
        $this->endpointNamespace = $endpointNamespace;
16
    }
17
18
    /**
19
     * @param string $name
20
     * @param string $version
21
     * @return Endpoint
22
     * @throws UnknownEndpointException
23
     */
24
    public function getEndpoint(string $name, string $version): Endpoint {
25
        $className = $this->buildVersionedEndpointNamespace($version) . $name;
26
        return new $className();
27
    }
28
29
    /**
30
     * WARNING: Only will find already loaded classes (Won't work with autoloaders). Meant as a basic example.
31
     * @param string $version
32
     * @return string[]
33
     */
34
    public function getSupportedEndpoints(string $version): array {
35
        $classesInNamespace = [];
36
37
        $namespace = $this->buildVersionedEndpointNamespace($version);
38
        foreach(get_declared_classes() as $name) {
39
            if (strpos($name, $namespace) === 0) {
40
                $classesInNamespace[] = $name;
41
            }
42
        }
43
44
        return $classesInNamespace;
45
    }
46
47
    protected function buildVersionedEndpointNamespace(string $version): string {
48
        return $this->endpointNamespace . '\Endpoints\v' . str_replace('.', '_', $version) . '\\';
49
    }
50
}
51