Passed
Push — master ( f3ac5e...2b5089 )
by Anatoly
50s queued 10s
created

CollectableFileLoader::attachArray()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
c 0
b 0
f 0
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 Sunrise\Http\Router\Exception\InvalidLoaderResourceException;
18
use Sunrise\Http\Router\RouteCollectionFactory;
19
use Sunrise\Http\Router\RouteCollectionFactoryInterface;
20
use Sunrise\Http\Router\RouteCollectionInterface;
21
use Sunrise\Http\Router\RouteCollector;
22
use Sunrise\Http\Router\RouteFactory;
23
use Sunrise\Http\Router\RouteFactoryInterface;
24
25
/**
26
 * Import functions
27
 */
28
use function glob;
29
use function is_dir;
30
use function is_file;
31
use function sprintf;
32
33
/**
34
 * CollectableFileLoader
35
 */
36
class CollectableFileLoader implements LoaderInterface
37
{
38
39
    /**
40
     * @var string[]
41
     */
42
    private $resources = [];
43
44
    /**
45
     * @var RouteCollectionFactoryInterface
46
     */
47
    private $collectionFactory;
48
49
    /**
50
     * @var RouteFactoryInterface
51
     */
52
    private $routeFactory;
53
54
    /**
55
     * Constructor of the class
56
     *
57
     * @param null|RouteCollectionFactoryInterface $collectionFactory
58
     * @param null|RouteFactoryInterface $routeFactory
59
     */
60 5
    public function __construct(
61
        RouteCollectionFactoryInterface $collectionFactory = null,
62
        RouteFactoryInterface $routeFactory = null
63
    ) {
64 5
        $this->collectionFactory = $collectionFactory ?? new RouteCollectionFactory();
65 5
        $this->routeFactory = $routeFactory ?? new RouteFactory();
66 5
    }
67
68
    /**
69
     * {@inheritDoc}
70
     */
71 4
    public function attach($resource) : void
72
    {
73 4
        if (is_dir($resource)) {
74 1
            $resources = glob($resource . '/*.php');
75
76 1
            foreach ($resources as $resource) {
0 ignored issues
show
introduced by
$resource is overwriting one of the parameters of this function.
Loading history...
77 1
                $this->resources[] = $resource;
78
            }
79
80 1
            return;
81
        }
82
83 3
        if (!is_file($resource)) {
84 1
            throw new InvalidLoaderResourceException(
85 1
                sprintf('The resource "%s" is not found.', $resource)
86
            );
87
        }
88
89 2
        $this->resources[] = $resource;
90 2
    }
91
92
    /**
93
     * {@inheritDoc}
94
     */
95 1
    public function attachArray(array $resources) : void
96
    {
97 1
        foreach ($resources as $resource) {
98 1
            $this->attach($resource);
99
        }
100 1
    }
101
102
    /**
103
     * {@inheritDoc}
104
     */
105 3
    public function load() : RouteCollectionInterface
106
    {
107 3
        $collect = new RouteCollector(
108 3
            $this->collectionFactory,
109 3
            $this->routeFactory
110
        );
111
112 3
        foreach ($this->resources as $resource) {
113
            (function () use ($resource) {
114 3
                require $resource;
115 3
            })->call($collect);
116
        }
117
118 3
        return $collect->getCollection();
119
    }
120
}
121