Completed
Push — master ( c7deb4...314746 )
by Beñat
09:20 queued 06:49
created

RoutesLoaderBuilder::defaultUploadDir()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 3
rs 10
cc 1
eloc 1
nc 1
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['upload_dir']) {
89
                $configuration[$key]['upload_dir'] = $this->defaultUploadDir($key);
90
            }
91
        }
92
93
        return $configuration;
94
    }
95
96
    /**
97
     * Gets the route loader's default upload dir.
98
     *
99
     * @param string $file The file name
100
     *
101
     * @return string
102
     */
103
    protected function defaultUploadDir($file)
104
    {
105
    }
106
107
    /**
108
     * Gets the service definition name.
109
     *
110
     * @return string
111
     */
112
    abstract protected function definitionName();
113
}
114