Completed
Push — 15.x ( 66d904...a48227 )
by Tim
01:30
created

ConfigurationFactory::getConfigurationParser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/**
4
 * TechDivision\Import\Configuration\Jms\ConfigurationFactory
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Tim Wagner <[email protected]>
15
 * @copyright 2016 TechDivision GmbH <[email protected]>
16
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
17
 * @link      https://github.com/techdivision/import-configuration-jms
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Configuration\Jms;
22
23
use TechDivision\Import\ConfigurationFactoryInterface;
24
use TechDivision\Import\Configuration\Jms\Configuration\Params;
25
use TechDivision\Import\Configuration\Jms\Serializer\SerializerBuilder;
26
27
/**
28
 * The configuration factory implementation.
29
 *
30
 * @author    Tim Wagner <[email protected]>
31
 * @copyright 2016 TechDivision GmbH <[email protected]>
32
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
33
 * @link      https://github.com/techdivision/import-configuration-jms
34
 * @link      http://www.techdivision.com
35
 */
36
class ConfigurationFactory implements ConfigurationFactoryInterface
37
{
38
39
    /**
40
     * The configuration parser factory instance used to create a parser that
41
     * parsers and merges the configuration from different directories.
42
     *
43
     * @var \TechDivision\Import\Configuration\Jms\ConfigurationParserFactoryInterface
44
     */
45
    protected $configurationParserFactory;
46
47
    /**
48
     * The configuration class name to use.
49
     *
50
     * @var string
51
     */
52
    protected $configurationClassName;
53
54
    /**
55
     * Initializes the instance with the configuration parser factory instance.
56
     *
57
     * @param \TechDivision\Import\Configuration\Jms\ConfigurationParserFactoryInterface $configurationParserFactory The configuration parser factory instance
58
     * @param string                                                                     $configurationClass         The configuration class name to use
0 ignored issues
show
Documentation introduced by
There is no parameter named $configurationClass. Did you maybe mean $configurationClassName?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
59
     */
60
    public function __construct(ConfigurationParserFactoryInterface $configurationParserFactory, $configurationClassName = Configuration::class)
61
    {
62
        $this->configurationParserFactory = $configurationParserFactory;
63
        $this->configurationClassName = $configurationClassName;
64
    }
65
66
    /**
67
     * Return's the configuration parser factory instance.
68
     *
69
     * @return \TechDivision\Import\Configuration\Jms\ConfigurationParserFactoryInterface The configuration parser factory instance
70
     */
71
    protected function getConfigurationParserFactory()
72
    {
73
        return $this->configurationParserFactory;
74
    }
75
76
    /**
77
     * Return's the configuration class name to use.
78
     *
79
     * @return string The configuration class name
80
     */
81
    protected function getConfigurationClassName()
82
    {
83
        return $this->configurationClassName;
84
    }
85
86
    /**
87
     * Returns the configuration parser for the passed format.
88
     *
89
     * @param string $format The format of the configuration file to return the parser for (either one of json, yaml or xml)
90
     *
91
     * @return \TechDivision\Import\Configuration\Jms\ConfigurationParserInterface The configuration parser instance
92
     */
93
    protected function getConfigurationParser($format)
94
    {
95
        return $this->getConfigurationParserFactory()->factory($format);
96
    }
97
98
    /**
99
     * Factory implementation to create a new initialized configuration instance.
100
     *
101
     * @param string $filename   The configuration filename
102
     * @param string $format     The format of the configuration file, either one of json, yaml or xml
103
     * @param string $params     A serialized string with additional params that'll be passed to the configuration
104
     * @param string $paramsFile A filename that contains serialized data with additional params that'll be passed to the configuration
105
     *
106
     * @return \TechDivision\Import\Configuration\Jms\Configuration The configuration instance
107
     * @throws \Exception Is thrown, if the specified configuration file doesn't exist
108
     */
109
    public function factory($filename, $format = 'json', $params = null, $paramsFile = null)
110
    {
111
112
        // try to load the JSON data
113
        if ($data = file_get_contents($filename)) {
114
            // initialize the JMS serializer, load and return the configuration
115
            return $this->factoryFromString($data, $format, $params, $paramsFile);
116
        }
117
118
        // throw an exception if the data can not be loaded from the passed file
119
        throw new \Exception(sprintf('Can\'t load configuration file %s', $filename));
120
    }
121
122
    /**
123
     * Factory implementation to create a new initialized configuration instance from a file
124
     * with configurations that'll be parsed and merged.
125
     *
126
     * @param array  $directories An array with diretories to parse and merge
127
     * @param string $format      The format of the configuration file, either one of json, yaml or xml
128
     * @param string $params      A serialized string with additional params that'll be passed to the configuration
129
     * @param string $paramsFile  A filename that contains serialized data with additional params that'll be passed to the configuration
130
     *
131
     * @return void
132
     */
133
    public function factoryFromDirectories(array $directories = array(), $format = 'json', $params = null, $paramsFile = null)
134
    {
135
        return $this->factoryFromString($this->getConfigurationParser($format)->parse($directories), $format, $params, $paramsFile);
136
    }
137
138
    /**
139
     * Factory implementation to create a new initialized configuration instance.
140
     *
141
     * @param string $data       The configuration data
142
     * @param string $format     The format of the configuration data, either one of json, yaml or xml
143
     * @param string $params     A serialized string with additional params that'll be passed to the configuration
144
     * @param string $paramsFile A filename that contains serialized data with additional params that'll be passed to the configuration
145
     *
146
     * @return \TechDivision\Import\ConfigurationInterface The configuration instance
147
     */
148
    public function factoryFromString($data, $format = 'json', $params = null, $paramsFile = null)
149
    {
150
151
        // initialize the JMS serializer, load and return the configuration
152
        $data = $this->toArray($data, $this->getConfigurationClassName(), $format);
153
154
        // merge the params, if specified with the --params option
155
        if ($params) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $params of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
156
            $this->replaceParams(
157
                $data,
158
                $this->toArray(
159
                    $params,
160
                    Params::class,
161
                    $format
162
                )
163
            );
164
        }
165
166
        // merge the param loaded from the file, if specified with the --params-file option
167
        if ($paramsFile && is_file($paramsFile)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $paramsFile of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
168
            $this->replaceParams(
169
                $data,
170
                $this->toArray(
171
                    file_get_contents($paramsFile),
172
                    Params::class,
173
                    pathinfo($paramsFile, PATHINFO_EXTENSION)
174
                )
175
            );
176
        }
177
178
        // finally, create and return the configuration from the merge data
179
        return SerializerBuilder::create()->build()->fromArray($data, $this->getConfigurationClassName());
180
    }
181
182
    /**
183
     * Creates and returns an array/object tree from the passed array.
184
     *
185
     * @param array  $data The data to create the object tree from
186
     * @param string $type The object type to create
187
     *
188
     * @return mixed The array/object tree from the passed array
189
     */
190
    protected function fromArray(array $data, $type)
191
    {
192
        return SerializerBuilder::create()->build()->fromArray($data, $type);
193
    }
194
195
    /**
196
     * Deserializes the data, converts it into an array and returns it.
197
     *
198
     * @param string $data   The data to convert
199
     * @param string $type   The object type for the deserialization
200
     * @param string $format The data format, either one of JSON, XML or YAML
201
     *
202
     * @return array The data as array
203
     */
204
    protected function toArray($data, $type, $format)
205
    {
206
207
        // load the serializer builde
208
        $serializer = SerializerBuilder::create()->build();
209
210
        // deserialize the data, convert it into an array and return it
211
        return $serializer->toArray($serializer->deserialize($data, $type, $format));
212
    }
213
214
    /**
215
     * Merge the additional params in the passed configuration data and replaces existing ones with the same name.
216
     *
217
     * @param array $data   The array with configuration data
218
     * @param array $params The array with additional params to merge
219
     *
220
     * @return void
221
     */
222
    protected function replaceParams(&$data, $params)
223
    {
224
        $data = array_replace_recursive($data, $params);
225
    }
226
}
227