AbstractExtension   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 156
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
dl 0
loc 156
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 9

9 Methods

Rating   Name   Duplication   Size   Complexity  
A initialize() 0 7 1
A getConfigDefinition() 0 15 3
A setConfigDefinition() 0 4 1
A processConfiguration() 0 4 1
A getAlias() 0 6 1
load() 0 1 ?
A prependConfiguration() 0 4 1
B loadResource() 0 17 5
A getBundlePath() 0 11 2
1
<?php
2
/*
3
 * This file is part of the Borobudur-Kernel package.
4
 *
5
 * (c) Hexacodelabs <http://hexacodelabs.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace Borobudur\Kernel\Bundling;
12
13
use Borobudur\Config\ConfigDefinitionInterface;
14
use Borobudur\Config\ConfigParser;
15
use Borobudur\Config\Configuration;
16
use Borobudur\Config\FileLoader;
17
use Borobudur\Config\FileLoader\FileLocator;
18
use Borobudur\DependencyInjection\ContainerBuilder;
19
use Borobudur\Kernel\IdentifierTrait;
20
use Borobudur\Kernel\ResourceBuilder;
21
22
/**
23
 * @author      Iqbal Maulana <[email protected]>
24
 * @created     8/13/15
25
 */
26
abstract class AbstractExtension
27
{
28
    use IdentifierTrait;
29
30
    /**
31
     * @var Configuration
32
     */
33
    private $config;
34
35
    /**
36
     * @var ConfigDefinitionInterface
37
     */
38
    private $configDefinition;
39
40
    /**
41
     * @var ContainerBuilder
42
     */
43
    private $container;
44
45
    /**
46
     * @var ResourceBuilder
47
     */
48
    private $resource;
49
50
    /**
51
     * @var FileLoader
52
     */
53
    private $loader;
54
55
    /**
56
     * Initialize extension.
57
     *
58
     * @param Configuration    $config
59
     * @param ContainerBuilder $container
60
     */
61
    public function initialize(Configuration $config, ContainerBuilder $container)
62
    {
63
        $this->config = $config;
64
        $this->container = $container;
65
        $this->resource = $container->get('resource');
66
        $this->loader = new FileLoader();
67
    }
68
69
    /**
70
     * @return ConfigDefinitionInterface|null
71
     */
72
    final public function getConfigDefinition()
73
    {
74
        if (null !== $this->configDefinition) {
75
            return $this->configDefinition;
76
        }
77
78
        $name = str_replace('Extension', '', $this->getName());
79
        $class = sprintf('%s\%s', $this->getNamespace(), $name . 'ConfigDefinition');
80
81
        if (class_exists($class)) {
82
            return new $class();
83
        }
84
85
        return null;
86
    }
87
88
    /**
89
     * Set config definition.
90
     *
91
     * @param ConfigDefinitionInterface $configDefinition
92
     */
93
    protected function setConfigDefinition(ConfigDefinitionInterface $configDefinition)
94
    {
95
        $this->configDefinition = $configDefinition;
96
    }
97
98
    /**
99
     * Parse config.
100
     *
101
     * @param array $configs
102
     *
103
     * @return array
104
     */
105
    final public function processConfiguration(array $configs)
106
    {
107
        return ConfigParser::parseConfiguration($this->getConfigDefinition(), $configs);
0 ignored issues
show
Documentation introduced by
$this->getConfigDefinition() is of type object|null, but the function expects a object<Borobudur\Config\...figDefinitionInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
108
    }
109
110
    /**
111
     * Get extension alias.
112
     *
113
     * @return string
114
     */
115
    final public function getAlias()
116
    {
117
        $name = str_replace('Extension', '', $this->getName());
118
119
        return convert_to_snake_case($name);
120
    }
121
122
    /**
123
     * Load extension.
124
     *
125
     * @param array            $configs
126
     * @param ContainerBuilder $container
127
     */
128
    abstract public function load(array $configs, ContainerBuilder $container);
129
130
    /**
131
     * Prepend configuration.
132
     *
133
     * @param array $configs
134
     */
135
    final protected function prependConfiguration(array $configs)
136
    {
137
        $this->config->prepend($this->getConfigDefinition(), $configs);
0 ignored issues
show
Documentation introduced by
$this->getConfigDefinition() is of type object|null, but the function expects a object<Borobudur\Config\...figDefinitionInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
138
    }
139
140
    /**
141
     * Load resource file.
142
     *
143
     * @param string $fileName
144
     *
145
     * @return array
146
     */
147
    final protected function loadResource($fileName)
148
    {
149
        $resolver = $this->container->getParameterBag()->getResolver();
150
        $fileName = $resolver->resolveString($this->getBundlePath().'/'.ltrim($fileName, '/'));
151
        $resources = $this->loader->import(new FileLocator($fileName));
152
        $resources = $this->resource->process($this->loader, $resources);
153
154
        if (isset($resources['parameters']) && is_array($resources['parameters'])) {
155
            foreach ($resources['parameters'] as $name => $param) {
156
                if (!$this->container->getParameterBag()->has($name)) {
157
                    $this->container->getParameterBag()->set($name, $param);
158
                }
159
            }
160
        }
161
162
        return $resolver->resolveValue($resources);
163
    }
164
165
    /**
166
     * Get bundle path.
167
     *
168
     * @return string
169
     */
170
    private function getBundlePath()
171
    {
172
        $path = $this->getPath();
173
        $pos = strrpos($path, '/');
174
175
        if (false === $pos) {
176
            return $path;
177
        }
178
179
        return substr($path, 0, $pos);
180
    }
181
}
182