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

RouteAttributeConfigurator::configure()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 3
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 3
rs 10
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