Payone_Config_Abstract::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
/**
3
 *
4
 * NOTICE OF LICENSE
5
 *
6
 * This source file is subject to the GNU General Public License (GPL 3)
7
 * that is bundled with this package in the file LICENSE.txt
8
 *
9
 * DISCLAIMER
10
 *
11
 * Do not edit or add to this file if you wish to upgrade Payone to newer
12
 * versions in the future. If you wish to customize Payone for your
13
 * needs please refer to http://www.payone.de for more information.
14
 *
15
 * Configuration for Payone SDK
16
 *
17
 * <b>Example: Replacing the default logging mechanism for Payone Api</b>
18
 * <pre  class="prettyprint">
19
 * $config = new Payone_Config();
20
 *
21
 * // Array with classname of logger to use as key, options array as value:
22
 * $myLoggers = array(
23
 *      'Payone_Protocol_Logger_Log4php' => array(
24
 *          'filename' => 'my_path/my_logfile.log',
25
 *          'max_file_size' => '50MB',
26
 *          'max_file_count' => 10));
27
 *
28
 * $config->setValue('api/default/protocol/loggers/', $myLoggers);
29
 * </pre>
30
 *
31
 * <b>Example: Adding an additional logger for Payone TransactionStatus</b>
32
 * <pre  class="prettyprint">
33
 * $config = new Payone_Config();
34
 *
35
 *  // options array:
36
 * $myLogger = array(
37
 *          'filename' => 'my_path/my_logfile.log',
38
 *          'max_file_size' => '50MB',
39
 *          'max_file_count' => 10));
40
 *
41
 *
42
 * $config->setValue('transaction_status/default/protocol/loggers/My_Logger_Class', $myLogger); *
43
 * </pre>
44
 *
45
 *
46
 * <b>Example: Changing the target log file for all logging activities</b>
47
 * <pre  class="prettyprint">
48
 *
49
 * // Initiate default config:
50
 * $config = new Payone_Config();
51
 *
52
 * // Change the log file only:
53
 * $config->setValue('api/default/protocol/loggers/Payone_Protocol_Logger_Log4php/filename', 'my_file.log'); *
54
 * </pre>
55
 *
56
 * @category        Payone
57
 * @package         Payone_Config
58
 * @copyright       Copyright (c) 2012 <[email protected]> - www.noovias.com
59
 * @author          Matthias Walter <[email protected]>
60
 * @license         <http://www.gnu.org/licenses/> GNU General Public License (GPL 3)
61
 * @link            http://www.noovias.com
62
 */
63
abstract class Payone_Config_Abstract
64
{
65
    const KEY_SEPARATOR = '/';
66
    /** @var array */
67
    protected $config = array();
68
69
    abstract protected function getDefaultConfigData();
70
71
    /**
72
     * @constructor
73
     *
74
     * @param array $data
75
     */
76
    public function __construct(array $data = array())
77
    {
78
        if (empty($data)) {
79
            $this->config = $this->getDefaultConfigData();
80
        }
81
        else {
82
            $this->config = $data;
83
        }
84
    }
85
86
    /**
87
     * Retrieve a value from the config
88
     * @param string $key Config key in the form 'node/node/node'
89
     * @return mixed
90
     */
91
    public function getValue($key)
92
    {
93
        return $this->get($key, $this->config);
94
    }
95
96
    /**
97
     * @param string $key Config key in the form 'node/node/node'
98
     * @param mixed $value Config to set
99
     */
100
    public function setValue($key, $value)
101
    {
102
        $this->set($key, $value, $this->config);
103
    }
104
105
    /**
106
     * @param string $key  Key in the form 'something/something'
107
     * @param mixed $value The value to set, can be any type
108
     * @param array $tree
109
     *
110
     * @return bool TRUE on Success
111
     */
112
    protected function set($key, $value, array &$tree)
113
    {
114
        if (strpos($key, self::KEY_SEPARATOR) !== FALSE
115
            // and is_array($tree)
0 ignored issues
show
Unused Code Comprehensibility introduced by
58% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
116
        ) {
117
            // Disassemble key, extracting the first node of the string:
118
            $explodedKey = explode(self::KEY_SEPARATOR, $key);
119
            $currentKey = array_shift($explodedKey);
120
121
            // Reassemble shortened key:
122
            $newKey = implode(self::KEY_SEPARATOR, $explodedKey);
123
            if (FALSE === array_key_exists($currentKey, $tree)) {
124
                // Create new array index:
125
                $tree[$currentKey] = array();
126
            }
127
128
            // Start recursion:
129
            return $this->set($newKey, $value, $tree[$currentKey]);
130
        }
131
        else {
132
            // Set value (can overwrite an existing value)
133
            $tree[$key] = $value;
134
            // Exit recursion, Success!
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
135
            return TRUE;
136
        }
137
    }
138
139
140
    /**
141
     * Recursively read from a nested array with a key/path
142
     * If a non-existant key is given, NULL will be returned.
143
     * Retrieving sub-trees is possible as well.
144
     *
145
     * Example:
146
     * get('root/node/node', array($root => array($node => array($node => 'value'))))
147
     * will return 'value'
148
     *
149
     * @recursive
150
     *
151
     * @param $key
152
     * @param array|mixed$tree An array, or, if recursively called, a leaf of the treef
153
     * @return mixed
154
     */
155
    protected function get($key, $tree)
156
    {
157
        if (strpos($key, self::KEY_SEPARATOR) !== FALSE and is_array($tree)) {
158
            // Disassemble key, extracting the first node of the string:
159
            $explodedKey = explode(self::KEY_SEPARATOR, $key);
160
            $currentKey = array_shift($explodedKey);
161
162
            if (array_key_exists($currentKey, $tree)) {
163
                // Get the node from the tree:
164
                $newTree = $tree[$currentKey];
165
166
                // Reassemble key, start recursion:
167
                $newKey = implode(self::KEY_SEPARATOR, $explodedKey);
168
                return $this->get($newKey, $newTree);
169
            }
170
            else {
171
                return NULL; // Exit recursion, unsuccessful
172
            }
173
        }
174
        elseif (is_array($tree) and array_key_exists($key, $tree)) {
175
            return $tree[$key]; // Exit recursion, Success!
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
176
        }
177
        else {
178
            return NULL; // Exit recursion, unsuccessful
179
        }
180
    }
181
182
    /**
183
     * @param array $config
184
     */
185
    public function setConfig($config)
186
    {
187
        $this->config = $config;
188
    }
189
190
    /**
191
     * @return array
192
     */
193
    public function getConfig()
194
    {
195
        return $this->config;
196
    }
197
198
}
199