Passed
Push — main ( 659659...7cf3e5 )
by Jeroen
09:29
created

RouteAttributeConfigurator::configureForClassName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 9
ccs 7
cts 7
cp 1
crap 2
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Jerowork\RouteAttributeProvider;
6
7
use Jerowork\FileClassReflector\FileFinder\RegexIterator\RegexIteratorFileFinder;
8
use Jerowork\FileClassReflector\PhpDocumentor\PhpDocumentorClassReflectorFactory;
9
use Jerowork\RouteAttributeProvider\RouteLoader\Cache\CacheRouteLoaderDecorator;
10
use Jerowork\RouteAttributeProvider\RouteLoader\ClassReflector\ClassReflectorRouteLoader;
11
use Jerowork\RouteAttributeProvider\RouteLoader\RouteLoaderInterface;
12
use phpDocumentor\Reflection\Php\ProjectFactory;
13
use Psr\SimpleCache\CacheInterface;
14
15
final class RouteAttributeConfigurator
16
{
17
    /**
18
     * @var string[] $directories
19
     */
20
    private array $directories = [];
21
22
    public function __construct(
23
        private RouteAttributeProviderInterface $routeAttributeProvider,
24
        private ?RouteLoaderInterface $routeLoader = null
25 3
    ) {
26
        $this->routeLoader = $this->routeLoader ?? new ClassReflectorRouteLoader(
27
                new PhpDocumentorClassReflectorFactory(
28
                    ProjectFactory::createInstance(),
29
                    new RegexIteratorFileFinder()
30 3
                )
31 3
            );
32 3
    }
33 3
34 3
    public function addDirectory(string ...$directories): self
35
    {
36 1
        foreach ($directories as $directory) {
37
            $this->directories[] = $directory;
38 1
        }
39 1
40
        return $this;
41
    }
42 1
43
    public function enableCache(CacheInterface $cache): self
44
    {
45 1
        $this->routeLoader = new CacheRouteLoaderDecorator($this->routeLoader, $cache);
0 ignored issues
show
Bug introduced by
It seems like $this->routeLoader can also be of type null; however, parameter $routeLoader of Jerowork\RouteAttributeP...ecorator::__construct() does only seem to accept Jerowork\RouteAttributeP...er\RouteLoaderInterface, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

45
        $this->routeLoader = new CacheRouteLoaderDecorator(/** @scrutinizer ignore-type */ $this->routeLoader, $cache);
Loading history...
46
47 1
        return $this;
48
    }
49 1
50
    public function configure(): void
51
    {
52 1
        foreach ($this->routeLoader->addDirectory(...$this->directories)->getRoutes() as $loadedRoute) {
0 ignored issues
show
Bug introduced by
The method addDirectory() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

52
        foreach ($this->routeLoader->/** @scrutinizer ignore-call */ addDirectory(...$this->directories)->getRoutes() as $loadedRoute) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
53
            $this->routeAttributeProvider->configure(
54 1
                $loadedRoute->getClassName(),
55 1
                $loadedRoute->getMethodName(),
56 1
                $loadedRoute->getRoute()
57
            );
58
        }
59 1
    }
60
61 2
    public function getRouteLoader(): RouteLoaderInterface
62
    {
63 2
        return $this->routeLoader;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->routeLoader could return the type null which is incompatible with the type-hinted return Jerowork\RouteAttributeP...er\RouteLoaderInterface. Consider adding an additional type-check to rule them out.
Loading history...
64
    }
65
}
66