Passed
Pull Request — master (#75)
by Anatoly
02:19
created

ConfigLoader   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 118
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 30
c 1
b 0
f 0
dl 0
loc 118
ccs 36
cts 36
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A getContainer() 0 3 1
A load() 0 15 2
A setContainer() 0 3 1
A attachArray() 0 4 2
A attach() 0 19 4
1
<?php declare(strict_types=1);
2
3
/**
4
 * It's free open-source software released under the MIT License.
5
 *
6
 * @author Anatoly Fenric <[email protected]>
7
 * @copyright Copyright (c) 2018, Anatoly Fenric
8
 * @license https://github.com/sunrise-php/http-router/blob/master/LICENSE
9
 * @link https://github.com/sunrise-php/http-router
10
 */
11
12
namespace Sunrise\Http\Router\Loader;
13
14
/**
15
 * Import classes
16
 */
17
use Psr\Container\ContainerInterface;
18
use Sunrise\Http\Router\Exception\InvalidLoaderResourceException;
19
use Sunrise\Http\Router\ReferenceResolver;
20
use Sunrise\Http\Router\ReferenceResolverInterface;
21
use Sunrise\Http\Router\RouteCollectionFactory;
22
use Sunrise\Http\Router\RouteCollectionFactoryInterface;
23
use Sunrise\Http\Router\RouteCollectionInterface;
24
use Sunrise\Http\Router\RouteCollector;
25
use Sunrise\Http\Router\RouteFactory;
26
use Sunrise\Http\Router\RouteFactoryInterface;
27
28
/**
29
 * Import functions
30
 */
31
use function glob;
32
use function is_dir;
33
use function is_file;
34
use function sprintf;
35
36
/**
37
 * ConfigLoader
38
 */
39
class ConfigLoader implements LoaderInterface
40
{
41
42
    /**
43
     * @var string[]
44
     */
45
    private $resources = [];
46
47
    /**
48
     * @var RouteCollectionFactoryInterface
49
     */
50
    private $collectionFactory;
51
52
    /**
53
     * @var RouteFactoryInterface
54
     */
55
    private $routeFactory;
56
57
    /**
58
     * @var ReferenceResolverInterface
59
     */
60
    private $referenceResolver;
61
62
    /**
63
     * Constructor of the class
64
     *
65
     * @param RouteCollectionFactoryInterface|null $collectionFactory
66
     * @param RouteFactoryInterface|null $routeFactory
67
     * @param ReferenceResolverInterface|null $referenceResolver
68
     */
69 8
    public function __construct(
70
        ?RouteCollectionFactoryInterface $collectionFactory = null,
71
        ?RouteFactoryInterface $routeFactory = null,
72
        ?ReferenceResolverInterface $referenceResolver = null
73
    ) {
74 8
        $this->collectionFactory = $collectionFactory ?? new RouteCollectionFactory();
75 8
        $this->routeFactory = $routeFactory ?? new RouteFactory();
76 8
        $this->referenceResolver = $referenceResolver ?? new ReferenceResolver();
77 8
    }
78
79
    /**
80
     * Gets the loader container
81
     *
82
     * @return ContainerInterface|null
83
     *
84
     * @since 2.9.0
85
     */
86 1
    public function getContainer() : ?ContainerInterface
87
    {
88 1
        return $this->referenceResolver->getContainer();
89
    }
90
91
    /**
92
     * Sets the given container to the loader
93
     *
94
     * @param ContainerInterface|null $container
95
     *
96
     * @return void
97
     *
98
     * @since 2.9.0
99
     */
100 2
    public function setContainer(?ContainerInterface $container) : void
101
    {
102 2
        $this->referenceResolver->setContainer($container);
103 2
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108 6
    public function attach($resource) : void
109
    {
110 6
        if (is_dir($resource)) {
111 1
            $resources = glob($resource . '/*.php');
112 1
            foreach ($resources as $resource) {
0 ignored issues
show
introduced by
$resource is overwriting one of the parameters of this function.
Loading history...
113 1
                $this->resources[] = $resource;
114
            }
115
116 1
            return;
117
        }
118
119 5
        if (!is_file($resource)) {
120 2
            throw new InvalidLoaderResourceException(sprintf(
121 2
                'The resource "%s" is not found.',
122 2
                $resource
123
            ));
124
        }
125
126 3
        $this->resources[] = $resource;
127 3
    }
128
129
    /**
130
     * {@inheritdoc}
131
     */
132 3
    public function attachArray(array $resources) : void
133
    {
134 3
        foreach ($resources as $resource) {
135 3
            $this->attach($resource);
136
        }
137 2
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142 4
    public function load() : RouteCollectionInterface
143
    {
144 4
        $collector = new RouteCollector(
145 4
            $this->collectionFactory,
146 4
            $this->routeFactory,
147 4
            $this->referenceResolver
148
        );
149
150 4
        foreach ($this->resources as $resource) {
151 4
            (function () use ($resource) {
152 4
                require $resource;
153 4
            })->call($collector);
154
        }
155
156 4
        return $collector->getCollection();
157
    }
158
}
159