Passed
Pull Request — master (#34)
by Anatoly
02:09
created

CollectableFileLoader::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 2
crap 1
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 is_file;
29
use function sprintf;
30
31
/**
32
 * CollectableFileLoader
33
 */
34
class CollectableFileLoader implements LoaderInterface
35
{
36
37
    /**
38
     * @var string[]
39
     */
40
    private $resources = [];
41
42
    /**
43
     * @var RouteCollectionFactoryInterface
44
     */
45
    private $collectionFactory;
46
47
    /**
48
     * @var RouteFactoryInterface
49
     */
50
    private $routeFactory;
51
52
    /**
53
     * Constructor of the class
54
     *
55
     * @param null|RouteCollectionFactoryInterface $collectionFactory
56
     * @param null|RouteFactoryInterface $routeFactory
57
     */
58 3
    public function __construct(
59
        RouteCollectionFactoryInterface $collectionFactory = null,
60
        RouteFactoryInterface $routeFactory = null
61
    ) {
62 3
        $this->collectionFactory = $collectionFactory ?? new RouteCollectionFactory();
63 3
        $this->routeFactory = $routeFactory ?? new RouteFactory();
64 3
    }
65
66
    /**
67
     * {@inheritDoc}
68
     */
69 2
    public function attach($resource) : void
70
    {
71 2
        if (!is_file($resource)) {
72 1
            throw new InvalidLoaderResourceException(
73 1
                sprintf('The resource "%s" is not found.', $resource)
74
            );
75
        }
76
77 1
        $this->resources[] = $resource;
78 1
    }
79
80
    /**
81
     * {@inheritDoc}
82
     */
83 1
    public function load() : RouteCollectionInterface
84
    {
85 1
        $collect = new RouteCollector(
86 1
            $this->collectionFactory,
87 1
            $this->routeFactory
88
        );
89
90 1
        foreach ($this->resources as $resource) {
91
            (function () use ($resource) {
92 1
                require $resource;
93 1
            })->call($collect);
94
        }
95
96 1
        return $collect->getCollection();
97
    }
98
}
99