Passed
Push — main ( a145c5...a6206c )
by Jeroen
02:18
created

RouteAttributeConfigurator   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
dl 0
loc 64
ccs 27
cts 27
cp 1
rs 10
c 1
b 0
f 0
wmc 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A getRouteLoader() 0 3 1
A configure() 0 5 3
A configureForClassName() 0 9 2
A enableCache() 0 5 1
A addDirectory() 0 7 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Jerowork\RouteAttributeProvider;
6
7
use Jerowork\RouteAttributeProvider\ClassNameLoader\ClassNameLoaderInterface;
8
use Jerowork\RouteAttributeProvider\ClassNameLoader\Tokenizer\TokenizerClassNameLoader;
9
use Jerowork\RouteAttributeProvider\RouteLoader\Cache\CacheRouteLoaderDecorator;
10
use Jerowork\RouteAttributeProvider\RouteLoader\Reflection\ReflectionRouteLoader;
11
use Jerowork\RouteAttributeProvider\RouteLoader\RouteLoaderInterface;
12
use Psr\SimpleCache\CacheInterface;
13
14
final class RouteAttributeConfigurator
15
{
16
    private RouteAttributeProviderInterface $routeAttributeProvider;
17
    private ClassNameLoaderInterface $classNameLoader;
18
    private RouteLoaderInterface $routeLoader;
19
20
    /**
21
     * @var string[] $directories
22
     */
23
    private array $directories;
24
25 3
    public function __construct(
26
        RouteAttributeProviderInterface $routeAttributeProvider,
27
        ?ClassNameLoaderInterface $classNameLoader = null,
28
        ?RouteLoaderInterface $routeLoader = null
29
    ) {
30 3
        $this->routeAttributeProvider = $routeAttributeProvider;
31 3
        $this->classNameLoader        = $classNameLoader ?? new TokenizerClassNameLoader();
32 3
        $this->routeLoader            = $routeLoader ?? new ReflectionRouteLoader();
33 3
        $this->directories            = [];
34 3
    }
35
36 1
    public function addDirectory(string ...$directories): self
37
    {
38 1
        foreach ($directories as $directory) {
39 1
            $this->directories[] = $directory;
40
        }
41
42 1
        return $this;
43
    }
44
45 1
    public function enableCache(CacheInterface $cache): self
46
    {
47 1
        $this->routeLoader = new CacheRouteLoaderDecorator($this->routeLoader, $cache);
48
49 1
        return $this;
50
    }
51
52 1
    public function configure(): void
53
    {
54 1
        foreach ($this->directories as $directory) {
55 1
            foreach ($this->classNameLoader->load($directory) as $className) {
56 1
                $this->configureForClassName($className);
57
            }
58
        }
59 1
    }
60
61 2
    public function getRouteLoader(): RouteLoaderInterface
62
    {
63 2
        return $this->routeLoader;
64
    }
65
66
    /**
67
     * @param class-string $className
0 ignored issues
show
Documentation Bug introduced by
The doc comment class-string at position 0 could not be parsed: Unknown type name 'class-string' at position 0 in class-string.
Loading history...
68
     */
69 1
    private function configureForClassName(string $className): void
70
    {
71 1
        $loadedRoutes = $this->routeLoader->load($className);
72
73 1
        foreach ($loadedRoutes as $loadedRoute) {
74 1
            $this->routeAttributeProvider->configure(
75 1
                $loadedRoute->getClassName(),
76 1
                $loadedRoute->getMethodName(),
77 1
                $loadedRoute->getRoute()
78
            );
79
        }
80 1
    }
81
}
82