Passed
Branch release/v2.0.0 (2af3f3)
by Anatoly
04:30
created

CollectableFileLoader   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A attach() 0 9 2
A load() 0 11 2
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\RouteCollectionInterface;
19
use Sunrise\Http\Router\RouteCollector;
20
21
/**
22
 * Import functions
23
 */
24
use function is_file;
25
use function sprintf;
26
27
/**
28
 * CollectableFileLoader
29
 */
30
class CollectableFileLoader implements LoaderInterface
31
{
32
33
    /**
34
     * @var string[]
35
     */
36
    private $resources = [];
37
38
    /**
39
     * {@inheritDoc}
40
     */
41 2
    public function attach($resource) : void
42
    {
43 2
        if (!is_file($resource)) {
44 1
            throw new InvalidLoadResourceException(
45 1
                sprintf('The "%s" resource not found.', $resource)
46
            );
47
        }
48
49 1
        $this->resources[] = $resource;
50 1
    }
51
52
    /**
53
     * {@inheritDoc}
54
     */
55 1
    public function load() : RouteCollectionInterface
56
    {
57 1
        $collect = new RouteCollector();
58
59 1
        foreach ($this->resources as $resource) {
60
            (function () use ($resource) {
61 1
                require $resource;
62 1
            })->call($collect);
63
        }
64
65 1
        return $collect->getCollection();
66
    }
67
}
68