Completed
Pull Request — master (#19)
by Tim
12:37
created

ConfigurationFactory::factoryFromString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
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 JMS\Serializer\SerializerBuilder;
24
use TechDivision\Import\ConfigurationFactoryInterface;
25
26
/**
27
 * The configuration factory implementation.
28
 *
29
 * @author    Tim Wagner <[email protected]>
30
 * @copyright 2016 TechDivision GmbH <[email protected]>
31
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
32
 * @link      https://github.com/techdivision/import-configuration-jms
33
 * @link      http://www.techdivision.com
34
 */
35
class ConfigurationFactory implements ConfigurationFactoryInterface
36
{
37
38
    /**
39
     * Factory implementation to create a new initialized configuration instance.
40
     *
41
     * @param string $filename The configuration filename
42
     * @param string $type     The format of the configuration file, either one of json, yaml or xml
43
     *
44
     * @return \TechDivision\Import\Configuration\Jms\Configuration The configuration instance
45
     * @throws \Exception Is thrown, if the specified configuration file doesn't exist
46
     */
47
    public function factory($filename, $type = 'json')
48
    {
49
50
        // try to load the JSON data
51
        if ($data = file_get_contents($filename)) {
52
            // initialize the JMS serializer, load and return the configuration
53
            return $this->factoryFromString($data, $type);
54
        }
55
56
        // throw an exception if the data can not be loaded from the passed file
57
        throw new \Exception(sprintf('Can\'t load configuration file %s', $filename));
58
    }
59
60
    /**
61
     * Factory implementation to create a new initialized configuration instance.
62
     *
63
     * @param string $data The configuration data
64
     * @param string $type The format of the configuration data, either one of json, yaml or xml
65
     *
66
     * @return \TechDivision\Import\Configuration\Jms\Configuration The configuration instance
67
     */
68
    public function factoryFromString($data, $type = 'json')
69
    {
70
        // initialize the JMS serializer, load and return the configuration
71
        return SerializerBuilder::create()->build()->deserialize($data, Configuration::class, $type);
72
    }
73
}
74