Completed
Push — master ( 5acd05...f1dd04 )
by Beñat
02:15 queued 34s
created

RoutesLoader::load()   C

Complexity

Conditions 8
Paths 9

Size

Total Lines 31
Code Lines 19

Duplication

Lines 6
Ratio 19.35 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 6
loc 31
rs 5.3846
cc 8
eloc 19
nc 9
nop 2
1
<?php
2
3
/*
4
 * This file is part of the BenGorFile package.
5
 *
6
 * (c) Beñat Espiña <[email protected]>
7
 * (c) Gorka Laucirica <[email protected]>
8
 *
9
 * For the full copyright and license information, please view the LICENSE
10
 * file that was distributed with this source code.
11
 */
12
13
namespace BenGorFile\FileBundle\Routing;
14
15
use Symfony\Component\Config\Loader\LoaderInterface;
16
use Symfony\Component\Config\Loader\LoaderResolverInterface;
17
use Symfony\Component\Routing\RouteCollection;
18
19
/**
20
 * Routes loader base class.
21
 *
22
 * Service that loads dynamically routes via PHP.
23
 *
24
 * @author Beñat Espiña <[email protected]>
25
 */
26
abstract class RoutesLoader implements LoaderInterface
27
{
28
    /**
29
     * Array which contains the
30
     * routes configuration.
31
     *
32
     * @var array
33
     */
34
    protected $config;
35
36
    /**
37
     * Boolean that checks if the
38
     * routes are already loaded or not.
39
     *
40
     * @var bool
41
     */
42
    protected $loaded;
43
44
    /**
45
     * Collection of routes.
46
     *
47
     * @var RouteCollection
48
     */
49
    protected $routes;
50
51
    /**
52
     * Constructor.
53
     *
54
     * @param array $config Array which contains the routes configuration
55
     */
56
    public function __construct(array $config = [])
57
    {
58
        $this->config = $config;
59
        $this->loaded = false;
60
    }
61
62
    /**
63
     * {@inheritdoc}
64
     */
65
    public function load($resource, $type = null)
66
    {
67
        if (true === $this->loaded) {
68
            throw new \RuntimeException('Do not add this loader twice');
69
        }
70
71
        $this->routes = new RouteCollection();
72
        if (empty($this->config)) {
73
            return $this->routes;
74
        }
75
76
        foreach ($this->config as $file => $config) {
77
            if (false === array_key_exists('enabled', $config)) {
78
                $this->register($file, $config);
79
                continue;
80
            }
81
            if (false === $config['enabled']) {
82
                continue;
83
            }
84 View Code Duplication
            if (true === array_key_exists('type', $config)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
85
                $config['type'] = $this->sanitize($config['type']);
86
            }
87 View Code Duplication
            if (true === array_key_exists('api_type', $config)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
88
                $config['api_type'] = $this->sanitize($config['api_type']);
89
            }
90
            $this->register($file, $config);
91
        }
92
        $this->loaded = true;
93
94
        return $this->routes;
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100
    public function getResolver()
101
    {
102
    }
103
104
    /**
105
     * {@inheritdoc}
106
     */
107
    public function setResolver(LoaderResolverInterface $resolver)
108
    {
109
    }
110
111
    /**
112
     * Sanitizes and validates the given specification name.
113
     *
114
     * @param string $specificationName The specification name
115
     *
116
     * @return string
117
     */
118
    protected function sanitize($specificationName)
119
    {
120
        return $specificationName;
121
    }
122
123
    /**
124
     * Registers a new route inside route
125
     * collection with the given params.
126
     *
127
     * @param string $file   The user name
128
     * @param array  $config The user configuration array
129
     */
130
    abstract protected function register($file, array $config);
131
}
132