Passed
Branch release/v2.0.0 (f10ed2)
by Anatoly
03:51
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\InvalidLoadResourceException;
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
     * @param null|RouteCollectionFactoryInterface $collectionFactory
54
     * @param null|RouteFactoryInterface $routeFactory
55
     */
56 3
    public function __construct(
57
        RouteCollectionFactoryInterface $collectionFactory = null,
58
        RouteFactoryInterface $routeFactory = null
59
    ) {
60 3
        $this->collectionFactory = $collectionFactory ?? new RouteCollectionFactory();
61 3
        $this->routeFactory = $routeFactory ?? new RouteFactory();
62 3
    }
63
64
    /**
65
     * {@inheritDoc}
66
     */
67 2
    public function attach($resource) : void
68
    {
69 2
        if (!is_file($resource)) {
70 1
            throw new InvalidLoadResourceException(
71 1
                sprintf('The "%s" resource not found.', $resource)
72
            );
73
        }
74
75 1
        $this->resources[] = $resource;
76 1
    }
77
78
    /**
79
     * {@inheritDoc}
80
     */
81 1
    public function load() : RouteCollectionInterface
82
    {
83 1
        $collect = new RouteCollector(
84 1
            $this->collectionFactory,
85 1
            $this->routeFactory
86
        );
87
88 1
        foreach ($this->resources as $resource) {
89
            (function () use ($resource) {
90 1
                require $resource;
91 1
            })->call($collect);
92
        }
93
94 1
        return $collect->getCollection();
95
    }
96
}
97