Test Failed
Push — master ( 0ee32e...41c938 )
by Dan
07:10
created

FileLoader::loadFile()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 23
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.5906
c 0
b 0
f 0
cc 6
eloc 12
nc 10
nop 1
1
<?php
2
/**
3
 * This file is part of the PSR Http 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
 * @link    https://github.com/djsmithme/Router
23
 */
24
class FileLoader extends AbstractLoader
25
{
26
    /**
27
     * File Loader Default settings
28
     *
29
     * @var array
30
     */
31
    public static $defaults = [
32
        'vars' => []
33
    ];
34
35
    /**
36
     * File Loader Options
37
     *
38
     * @var array
39
     */
40
    protected $options;
41
42
    /**
43
     * FileLoader constructor.
44
     *
45
     * @param RouterInterface $router Routing Component
46
     * @param array $options File Loader Options.
47
     */
48
    public function __construct(RouterInterface $router, array $options = [])
49
    {
50
        $this->options = \array_replace_recursive(FileLoader::$defaults, $options);
51
        $this->router = $router;
52
    }
53
54
    /**
55
     * @inheritdoc
56
     */
57 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...
58
    {
59
        if (!$this->router->isCached()) {
60
            foreach ((array)$files as $filename) {
61
                $collection = $this->loadFile($filename);
62
                $this->router = $this->router->mergeCollection($collection);
63
            }
64
        }
65
        return $this->router;
66
    }
67
68
    /**
69
     * @inheritdoc
70
     */
71
    public function loadFile(string $file)
72
    {
73
        if (!$this->router->isCached()) {
74
            foreach ((array)$this->options['vars'] as $key => $item) {
75
                if (\is_string($key)) {
76
                    ${$key} = $item;
77
                }
78
            }
79
80
            if (!\file_exists($file)) {
81
                throw new RouterException("Could not locate route file.'{$file}'");
82
            }
83
84
            $routeData = require $file;
85
86
            if ($routeData instanceof RouteCollectionInterface === false) {
87
                throw new RouterException("'{$file}' must return an instance of Rs\\Router\\RouteCollectionInterface");
88
            }
89
90
            return $routeData;
91
        }
92
        return $this->router->getCollection();
93
    }
94
}
95