Completed
Push — master ( 83b89e...48455b )
by Hannes
04:08 queued 02:15
created

RouteFactory   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 39
rs 10
c 0
b 0
f 0
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 2
A createRoutesFrom() 0 27 5
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace inroutephp\inroute\Compiler\Doctrine;
6
7
use inroutephp\inroute\Compiler\RouteCollectionInterface;
8
use inroutephp\inroute\Compiler\RouteCollection;
9
use inroutephp\inroute\Runtime\Route as RouteObject;
10
use Doctrine\Common\Annotations\AnnotationReader;
11
12
final class RouteFactory
13
{
14
    /**
15
     * @var AnnotationReader
16
     */
17
    private $reader;
18
19
    public function __construct(AnnotationReader $reader = null)
20
    {
21
        $this->reader = $reader ?: new AnnotationReader;
22
    }
23
24
    public function createRoutesFrom(string $classname): RouteCollectionInterface
25
    {
26
        if (!class_exists($classname)) {
27
            return new RouteCollection([]);
28
        }
29
30
        $classReflector = new \ReflectionClass($classname);
31
32
        if (!$classReflector->isInstantiable()) {
33
            return new RouteCollection([]);
34
        }
35
36
        $routes = [];
37
38
        foreach ($classReflector->getMethods(\ReflectionMethod::IS_PUBLIC) as $methodReflector) {
39
            if ($methodReflector->isConstructor()) {
40
                continue;
41
            }
42
43
            $routes[] = new RouteObject(
44
                $classReflector->getName(),
45
                $methodReflector->getName(),
46
                $this->reader->getMethodAnnotations($methodReflector)
47
            );
48
        }
49
50
        return new RouteCollection($routes);
51
    }
52
}
53