AnnotationRouteCollectionBuilder   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 28
c 1
b 0
f 0
dl 0
loc 61
ccs 0
cts 26
cp 0
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A buildCache() 0 8 2
A build() 0 30 3
1
<?php declare(strict_types = 1);
2
3
/**
4
 * This file is part of the Simplex package.
5
 *
6
 * (c) Freddie Frantzen <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
namespace Simplex\Routing;
12
13
use Doctrine\Common\Annotations\AnnotationReader;
14
use Doctrine\Common\Annotations\CachedReader;
15
use Doctrine\Common\Cache\ArrayCache;
16
use Doctrine\Common\Cache\CacheProvider;
17
use Doctrine\Common\Cache\FilesystemCache;
18
use Psr\Container\ContainerInterface;
19
use Simplex\ContainerKeys;
20
use Symfony\Component\Config\FileLocator;
21
use Symfony\Component\Routing\Loader\AnnotationDirectoryLoader;
22
use Symfony\Component\Routing\RouteCollection;
23
24
/**
25
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
26
 */
27
final class AnnotationRouteCollectionBuilder implements RouteCollectionBuilder
28
{
29
    private const ROUTING_CACHE_DIRECTORY = 'router';
30
31
    private const CONTROLLER_DIRECTORY = 'Controller';
32
33
    /** @var bool */
34
    private $enableCache;
35
36
    /** @var \SplFileInfo */
37
    private $cacheDirectory;
38
39
    public function __construct(bool $enableCache, \SplFileInfo $cacheDirectory)
40
    {
41
        $this->enableCache = $enableCache;
42
        $this->cacheDirectory = $cacheDirectory;
43
    }
44
45
    /**
46
     * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
47
     */
48
    public function build(ContainerInterface $container, array $modules): RouteCollection
49
    {
50
        $cache = $this->buildCache();
51
52
        $reader = new CachedReader(
53
            new AnnotationReader(),
54
            $cache,
55
            $container->get(ContainerKeys::DEBUG_MODE)
56
        );
57
58
        $fileLoader = new FileLocator();
59
        $annotationClassLoader = new AnnotatedRouteControllerLoader($reader);
60
        $annotationLoader = new AnnotationDirectoryLoader($fileLoader, $annotationClassLoader);
61
62
        $routeCollection = new RouteCollection();
63
64
        foreach ($modules as $module) {
65
            $reflector = new \ReflectionClass(get_class($module));
66
            $fileInfo = new \SplFileInfo($reflector->getFileName());
67
68
            $controllerDir = $fileInfo->getPath() . DIRECTORY_SEPARATOR . self::CONTROLLER_DIRECTORY;
69
70
            if (!is_readable($controllerDir)) {
71
                continue;
72
            }
73
74
            $routeCollection->addCollection($annotationLoader->load($controllerDir));
75
        }
76
77
        return $routeCollection;
78
    }
79
80
    private function buildCache(): CacheProvider
81
    {
82
        if (!$this->enableCache) {
83
            return new ArrayCache();
84
        }
85
86
        return new FilesystemCache(
87
            $this->cacheDirectory->getPathname() . DIRECTORY_SEPARATOR . self::ROUTING_CACHE_DIRECTORY
88
        );
89
    }
90
}
91