Completed
Pull Request — master (#94)
by Tim
05:44
created

ConfigurationUtil   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 60
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 60
c 0
b 0
f 0
wmc 7
lcom 0
cbo 0
ccs 0
cts 24
cp 0
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A __clone() 0 3 1
B prepareConstructorArgs() 0 29 5
1
<?php
2
3
/**
4
 * TechDivision\Import\Utils\ConfigurationUtil
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
18
 * @link      http://www.techdivision.com
19
 */
20
21
namespace TechDivision\Import\Utils;
22
23
/**
24
 * Utility class containing the configuration keys.
25
 *
26
 * @author    Tim Wagner <[email protected]>
27
 * @copyright 2016 TechDivision GmbH <[email protected]>
28
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
29
 * @link      https://github.com/techdivision/import
30
 * @link      http://www.techdivision.com
31
 */
32
class ConfigurationUtil
33
{
34
35
    /**
36
     * This is a utility class, so protect it against direct
37
     * instantiation.
38
     */
39
    private function __construct()
40
    {
41
    }
42
43
    /**
44
     * This is a utility class, so protect it against cloning.
45
     *
46
     * @return void
47
     */
48
    private function __clone()
49
    {
50
    }
51
52
    /**
53
     * Prepare's the arguments for the passed reflection class by applying the values from the passed configuration array
54
     * to the apropriate arguments. If no value is found in the configuration, the constructor argument's default value is
55
     * used.
56
     *
57
     * @param \ReflectionClass $reflectionClass The reflection class to prepare the arguments for
58
     * @param array            $params          The constructor arguments from the configuration
59
     *
60
     * @return array The array with the constructor arguements
61
     */
62
    public static function prepareConstructorArgs(\ReflectionClass $reflectionClass, array $params)
63
    {
64
65
        // prepare the array for the initialized arguments
66
        $initializedParams = array();
67
68
        // prepare the array for the arguments in camel case (in configuration we use a '-' notation)
69
        $paramsInCamelCase = array();
70
71
        // convert the configuration keys to camelcase
72
        foreach ($params as $key => $value) {
73
            $paramsInCamelCase[lcfirst(str_replace('-', '', ucwords($key, '-')))] = $value;
74
        }
75
76
        // prepare the arguments by applying the values from the configuration
77
        /** @var \ReflectionParameter $reflectionParameter */
78
        foreach ($reflectionClass->getConstructor()->getParameters() as $reflectionParameter) {
79
            if (isset($paramsInCamelCase[$paramName = $reflectionParameter->getName()])) {
80
                $initializedParams[$paramName] = $paramsInCamelCase[$paramName];
81
            } elseif ($reflectionParameter->isOptional()) {
82
                $initializedParams[$paramName] = $reflectionParameter->getDefaultValue();
83
            } else {
84
                $initializedParams[$paramName] = null;
85
            }
86
        }
87
88
        // return the array with the prepared arguments
89
        return $initializedParams;
90
    }
91
}
92