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

RoutesLoaderBuilder::sanitize()   B

Complexity

Conditions 6
Paths 17

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
rs 8.8571
cc 6
eloc 11
nc 17
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
            $this->container->getDefinition(
60
                $this->definitionName()
61
            )->replaceArgument(0, array_unique($this->configuration, SORT_REGULAR));
62
        }
63
        if ($this->container->hasDefinition($this->definitionApiName())) {
64
            foreach ($this->configuration as $key => $config) {
65
                $this->configuration[$key]['enabled'] = $config['api_enabled'];
66
                if (array_key_exists('type', $config)) {
67
                    $this->configuration[$key]['type'] = $config['api_type'];
68
                }
69
            }
70
            $this->container->getDefinition(
71
                $this->definitionApiName()
72
            )->replaceArgument(0, array_unique($this->configuration, SORT_REGULAR));
73
        }
74
        return $this->container;
75
    }
76
77
    /**
78
     * Gets the configuration after sanitize process.
79
     *
80
     * @return array
81
     */
82
    public function configuration()
83
    {
84
        return $this->configuration;
85
    }
86
87
    /**
88
     * Sanitizes and validates the given configuration tree.
89
     *
90
     * @param array $configuration The configuration tree
91
     *
92
     * @return array
93
     */
94
    protected function sanitize(array $configuration)
95
    {
96
        foreach ($configuration as $key => $config) {
97
            if (null === $config['name']) {
98
                $configuration[$key]['name'] = $this->defaultRouteName($key);
99
            }
100
            if (null === $config['path']) {
101
                $configuration[$key]['path'] = $this->defaultRoutePath($key);
102
            }
103
            if (null === $config['api_name']) {
104
                $configuration[$key]['api_name'] = $this->defaultApiRouteName($key);
105
            }
106
            if (null === $config['api_path']) {
107
                $configuration[$key]['api_path'] = $this->defaultApiRoutePath($key);
108
            }
109
        }
110
111
        return $configuration;
112
    }
113
114
    /**
115
     * Gets the route loader's default upload dir.
116
     *
117
     * @param string $file The file name
118
     *
119
     * @return string
120
     */
121
    protected function defaultUploadDir($file)
122
    {
123
    }
124
125
    /**
126
     * Gets the service definition name.
127
     *
128
     * @return string
129
     */
130
    abstract protected function definitionName();
131
132
    /**
133
     * Gets the route loader's default route name.
134
     *
135
     * @param string $file The file name
136
     *
137
     * @return string
138
     */
139
    protected function defaultRouteName($file)
140
    {
141
    }
142
143
    /**
144
     * Gets the route loader's default route path.
145
     *
146
     * @param string $file The file name
147
     *
148
     * @return string
149
     */
150
    protected function defaultRoutePath($file)
151
    {
152
    }
153
154
    /**
155
     * Gets the service definition API name.
156
     *
157
     * @return string
158
     */
159
    protected function definitionApiName()
160
    {
161
    }
162
163
    /**
164
     * Gets the route loader's default API route name.
165
     *
166
     * @param string $file The file name
167
     *
168
     * @return string
169
     */
170
    protected function defaultApiRouteName($file)
171
    {
172
    }
173
174
    /**
175
     * Gets the route loader's default API route path.
176
     *
177
     * @param string $file The file name
178
     *
179
     * @return string
180
     */
181
    protected function defaultApiRoutePath($file)
182
    {
183
    }
184
}
185