Passed
Push — main ( ec4e01...4c84a5 )
by Jeroen
02:35 queued 45s
created

RouteAttributeConfigurator   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 21
c 1
b 0
f 0
dl 0
loc 52
ccs 22
cts 22
cp 1
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 9 1
A configure() 0 5 3
A configureForClassName() 0 9 2
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\Reflection\ReflectionRouteLoader;
10
use Jerowork\RouteAttributeProvider\RouteLoader\RouteLoaderInterface;
11
12
final class RouteAttributeConfigurator
13
{
14
    private RouteAttributeProviderInterface $routeAttributeProvider;
15
    private ClassNameLoaderInterface $classNameLoader;
16
    private RouteLoaderInterface $routeLoader;
17
18
    /**
19
     * @var string[] $directories
20
     */
21
    private array $directories;
22
23 1
    public function __construct(
24
        RouteAttributeProviderInterface $routeAttributeProvider,
25
        ?ClassNameLoaderInterface $classNameLoader = null,
26
        ?RouteLoaderInterface $routeLoader = null
27
    ) {
28 1
        $this->routeAttributeProvider = $routeAttributeProvider;
29 1
        $this->classNameLoader        = $classNameLoader ?? new TokenizerClassNameLoader();
30 1
        $this->routeLoader            = $routeLoader ?? new ReflectionRouteLoader();
31 1
        $this->directories            = [];
32 1
    }
33
34 1
    public function addDirectory(string ...$directories) : self
35
    {
36 1
        foreach ($directories as $directory) {
37 1
            $this->directories[] = $directory;
38
        }
39
40 1
        return $this;
41
    }
42
43 1
    public function configure(): void
44
    {
45 1
        foreach ($this->directories as $directory) {
46 1
            foreach ($this->classNameLoader->load($directory) as $className) {
47 1
                $this->configureForClassName($className);
48
            }
49
        }
50 1
    }
51
52
    /**
53
     * @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...
54
     */
55 1
    private function configureForClassName(string $className): void
56
    {
57 1
        $loadedRoutes = $this->routeLoader->load($className);
58
59 1
        foreach ($loadedRoutes as $loadedRoute) {
60 1
            $this->routeAttributeProvider->configure(
61 1
                $loadedRoute->getClassName(),
62 1
                $loadedRoute->getMethodName(),
63 1
                $loadedRoute->getRoute()
64
            );
65
        }
66 1
    }
67
}
68