FileLoader::loadFiles()   A
last analyzed

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
dl 10
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 2
nop 1
crap 3
1
<?php
2
/**
3
 * This file is part of the DS Framework.
4
 *
5
 * (c) Dan Smith <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
namespace Ds\Router\Loaders;
11
12
use Ds\Router\Exceptions\RouterException;
13
use Ds\Router\Interfaces\RouteCollectionInterface;
14
use Ds\Router\Interfaces\RouterInterface;
15
16
/**
17
 * Class FileLoader
18
 *
19
 * @package Ds\Router\Loaders
20
 * @author  Dan Smith    <[email protected]>
21
 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
22
 */
23
class FileLoader extends AbstractLoader
24
{
25
    /**
26
     * File Loader Default settings
27
     *
28
     * @var array
29
     */
30
    public static $defaults = [
31
        'vars' => []
32
    ];
33
34
    /**
35
     * File Loader Options
36
     *
37
     * @var array
38
     */
39
    protected $options;
40
41
    /**
42
     * FileLoader constructor.
43
     *
44
     * @param RouterInterface $router Routing Component
45
     * @param array $options File Loader Options.
46
     */
47 7
    public function __construct(RouterInterface $router, array $options = [])
48
    {
49 7
        $this->options = \array_replace_recursive(FileLoader::$defaults, $options);
50 7
        $this->router = $router;
51 7
    }
52
53
    /**
54
     * @inheritdoc
55
     */
56 3 View Code Duplication
    public function loadFiles(array $files)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57
    {
58 3
        if (!$this->router->isCached()) {
59 2
            foreach ((array)$files as $filename) {
60 2
                $collection = $this->loadFile($filename);
61 2
                $this->router = $this->router->mergeCollection($collection);
62
            }
63
        }
64 2
        return $this->router;
65
    }
66
67
    /**
68
     * @inheritdoc
69
     */
70 6
    public function loadFile(string $file)
71
    {
72 6
        if (!$this->router->isCached()) {
73 5
            foreach ((array)$this->options['vars'] as $key => $item) {
74 5
                if (\is_string($key)) {
75 5
                    ${$key} = $item;
76
                }
77
            }
78
79 5
            if (!\file_exists($file)) {
80 2
                throw new RouterException("Could not locate route file.'{$file}'");
81
            }
82
83 4
            $routeData = require $file;
84
85 4
            if ($routeData instanceof RouteCollectionInterface === false) {
86 1
                throw new RouterException("'{$file}' must return an instance of Rs\\Router\\RouteCollectionInterface");
87
            }
88
89 3
            return $routeData;
90
        }
91 1
        return $this->router->getCollection();
92
    }
93
}
94