Completed
Pull Request — master (#6)
by Beñat
01:31
created

RoutesLoaderBuilder::sanitize()   C

Complexity

Conditions 7
Paths 33

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 22
rs 6.9811
c 0
b 0
f 0
cc 7
eloc 13
nc 33
nop 1
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\DependencyInjection\Compiler\Routing;
14
15
use Symfony\Component\DependencyInjection\ContainerBuilder;
16
17
/**
18
 * Base routes loader builder.
19
 *
20
 * @author Beñat Espiña <[email protected]>
21
 */
22
abstract class RoutesLoaderBuilder
23
{
24
    /**
25
     * Configuration array.
26
     *
27
     * @var array
28
     */
29
    protected $configuration;
30
31
    /**
32
     * The container builder.
33
     *
34
     * @var ContainerBuilder
35
     */
36
    protected $container;
37
38
    /**
39
     * Constructor.
40
     *
41
     * @param ContainerBuilder $container     The container builder
42
     * @param array            $configuration The configuration tree
43
     */
44
    public function __construct(ContainerBuilder $container, array $configuration = [])
45
    {
46
        $this->configuration = $this->sanitize($configuration);
47
        $this->container = $container;
48
    }
49
50
    /**
51
     * Entry point of routes loader builder to
52
     * inject routes inside route loader.
53
     *
54
     * @return ContainerBuilder
55
     */
56
    public function build()
57
    {
58
        if (!$this->container->hasDefinition($this->definitionName())) {
59
            return;
60
        }
61
        $this->container->getDefinition(
62
            $this->definitionName()
63
        )->replaceArgument(0, array_unique($this->configuration, SORT_REGULAR));
64
65
        return $this->container;
66
    }
67
68
    /**
69
     * Gets the configuration after sanitize process.
70
     *
71
     * @return array
72
     */
73
    public function configuration()
74
    {
75
        return $this->configuration;
76
    }
77
78
    /**
79
     * Sanitizes and validates the given configuration tree.
80
     *
81
     * @param array $configuration The configuration tree
82
     *
83
     * @return array
84
     */
85
    protected function sanitize(array $configuration)
86
    {
87
        foreach ($configuration as $key => $config) {
88
            if (null === $config['download_base_url']) {
89
                $configuration[$key]['download_base_url'] = $this->defaultUploadDir($key);
90
            }
91
            if (null === $config['name']) {
92
                $configuration[$key]['name'] = $this->defaultRouteName($key);
93
            }
94
            if (null === $config['path']) {
95
                $configuration[$key]['path'] = $this->defaultRoutePath($key);
96
            }
97
            if (null === $config['api_name']) {
98
                $configuration[$key]['api_name'] = $this->defaultApiRouteName($key);
99
            }
100
            if (null === $config['api_path']) {
101
                $configuration[$key]['api_path'] = $this->defaultApiRoutePath($key);
102
            }
103
        }
104
105
        return $configuration;
106
    }
107
108
    /**
109
     * Gets the route loader's default upload dir.
110
     *
111
     * @param string $file The file name
112
     *
113
     * @return string
114
     */
115
    protected function defaultUploadDir($file)
116
    {
117
    }
118
119
    /**
120
     * Gets the service definition name.
121
     *
122
     * @return string
123
     */
124
    abstract protected function definitionName();
125
126
    /**
127
     * Gets the route loader's default route name.
128
     *
129
     * @param string $file The file name
130
     *
131
     * @return string
132
     */
133
    protected function defaultRouteName($file)
134
    {
135
    }
136
137
    /**
138
     * Gets the route loader's default route path.
139
     *
140
     * @param string $file The file name
141
     *
142
     * @return string
143
     */
144
    protected function defaultRoutePath($file)
145
    {
146
    }
147
148
    /**
149
     * Gets the service definition API name.
150
     *
151
     * @return string
152
     */
153
    protected function definitionApiName()
154
    {
155
    }
156
157
    /**
158
     * Gets the route loader's default API route name.
159
     *
160
     * @param string $file The file name
161
     *
162
     * @return string
163
     */
164
    protected function defaultApiRouteName($file)
165
    {
166
    }
167
168
    /**
169
     * Gets the route loader's default API route path.
170
     *
171
     * @param string $file The file name
172
     *
173
     * @return string
174
     */
175
    protected function defaultApiRoutePath($file)
176
    {
177
    }
178
}
179