Payone_Config::getApiConfig()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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
 * @category        Payone
16
 * @package         Payone
17
 * @copyright       Copyright (c) 2012 <[email protected]> - www.noovias.com
18
 * @author          Matthias Walter <[email protected]>
19
 * @license         <http://www.gnu.org/licenses/> GNU General Public License (GPL 3)
20
 * @link            http://www.noovias.com
21
 */
22
23
/**
24
 * Configuration for Payone SDK
25
 *
26
 * <b>Example: Replacing the default logging mechanism for Payone Api</b>
27
 * <pre  class="prettyprint">
28
 * $config = new Payone_Config();
29
 *
30
 * // Array with classname of logger to use as key, options array as value:
31
 * $myLoggers = array(
32
 *      'Payone_Protocol_Logger_Log4php' => array(
33
 *          'filename' => 'my_path/my_logfile.log',
34
 *          'max_file_size' => '50MB',
35
 *          'max_file_count' => 10));
36
 *
37
 * $config->setValue('api/default/protocol/loggers/', $myLoggers);
38
 * </pre>
39
 *
40
 * <b>Example: Adding an additional logger for Payone TransactionStatus</b>
41
 * <pre  class="prettyprint">
42
 * $config = new Payone_Config();
43
 *
44
 *  // options array:
45
 * $myLogger = array(
46
 *          'filename' => 'my_path/my_logfile.log',
47
 *          'max_file_size' => '50MB',
48
 *          'max_file_count' => 10));
49
 *
50
 *
51
 * $config->setValue('transaction_status/default/protocol/loggers/My_Logger_Class', $myLogger); *
52
 * </pre>
53
 *
54
 *
55
 * <b>Example: Changing the target log file for all logging activities</b>
56
 * <pre  class="prettyprint">
57
 *
58
 * // Initiate default config:
59
 * $config = new Payone_Config();
60
 *
61
 * // Change the log file only:
62
 * $config->setValue('api/default/protocol/loggers/Payone_Protocol_Logger_Log4php/filename', 'my_file.log'); *
63
 * </pre>
64
 *
65
 * @category        Payone
66
 * @package         Payone
67
 * @copyright       Copyright (c) 2012 <[email protected]> - www.noovias.com
68
 * @license         <http://www.gnu.org/licenses/> GNU General Public License (GPL 3)
69
 * @link            http://www.noovias.com
70
 */
71
class Payone_Config
72
{
73
    const KEY_SEPARATOR = '/';
74
    /** @var array */
75
    protected $config = array();
76
77
    /** @var Payone_Api_Config */
78
    protected $apiConfig = null;
79
80
    /** @var Payone_TransactionStatus_Config */
81
    protected $transactionStatusConfig = null;
82
83
    /** @var Payone_SessionStatus_Config */
84
    protected $sessionStatusConfig = null;
85
86
    /**
87
     * @constructor
88
     *
89
     * @param array $data
90
     */
91
    public function __construct(array $data = array())
92
    {
93
        if (empty($data)) {
94
            if($this->getApiConfig() === null)
95
            {
96
                $this->apiConfig = new Payone_Api_Config();
97
            }
98
99
            if($this->getTransactionStatusConfig() === null)
100
            {
101
                $this->transactionStatusConfig = new Payone_TransactionStatus_Config();
102
            }
103
104
            if($this->getSessionStatusConfig() === null)
105
            {
106
                $this->sessionStatusConfig = new Payone_SessionStatus_Config();
107
            }
108
109
            $this->config = $this->getDefaultConfigData();
110
        }
111
        else {
112
            if(array_key_exists('api', $data))
113
                $this->setApiConfig($data['api']);
114
            if(array_key_exists('transaction_status', $data))
115
                $this->setTransactionStatusConfig($data['transaction_status']);
116
            if(array_key_exists('session_status', $data))
117
                $this->setSessionStatusConfig($data['session_status']);
118
            $this->config = $data;
119
        }
120
    }
121
122
    /**
123
     * Retrieve a value from the config
124
     * @param string $key Config key in the form 'node/node/node'
125
     * @return mixed
126
     */
127
    public function getValue($key)
128
    {
129
        return $this->get($key, $this->config);
130
    }
131
132
    /**
133
     * @param string $key Config key in the form 'node/node/node'
134
     * @param mixed $value Config to set
135
     */
136
    public function setValue($key, $value)
137
    {
138
        $this->set($key, $value, $this->config);
139
    }
140
141
    /**
142
     * @return array
143
     */
144
    protected function getDefaultConfigData()
145
    {
146
        $configData = array(
147
            'api' => $this->getApiConfig(),
148
            'transaction_status' => $this->getTransactionStatusConfig(),
149
            'session_status' => $this->getSessionStatusConfig()
150
        );
151
152
        return $configData;
153
    }
154
155
    /**
156
     * @param string $key  Key in the form 'something/something'
157
     * @param mixed $value The value to set, can be any type
158
     * @param array $tree
159
     *
160
     * @return bool TRUE on Success
161
     */
162
    protected function set($key, $value, array &$tree)
163
    {
164
        if (strpos($key, self::KEY_SEPARATOR) !== FALSE
165
            // 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...
166
        ) {
167
            // Disassemble key, extracting the first node of the string:
168
            $explodedKey = explode(self::KEY_SEPARATOR, $key);
169
            $currentKey = array_shift($explodedKey);
170
            $newKey = implode(self::KEY_SEPARATOR, $explodedKey);
171
172
            /** @var $object Payone_Config_Abstract  */
173
            $object = $tree[$currentKey];
174
            $object->setValue($newKey, $value);
175
            return TRUE;
176
        }
177
        else {
178
            // Set value (can overwrite an existing value)
179
            $tree[$key] = $value;
180
            // 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...
181
            return TRUE;
182
        }
183
    }
184
185
    /**
186
     * Recursively read from a nested array with a key/path
187
     * If a non-existant key is given, NULL will be returned.
188
     * Retrieving sub-trees is possible as well.
189
     *
190
     * Example:
191
     * get('root/node/node', array($root => array($node => array($node => 'value'))))
192
     * will return 'value'
193
     *
194
     * @recursive
195
     *
196
     * @param $key
197
     * @param array|mixed$tree An array, or, if recursively called, a leaf of the treef
198
     * @return mixed
199
     */
200
    protected function get($key, $tree)
201
    {
202
        if (strpos($key, self::KEY_SEPARATOR) !== FALSE and is_array($tree)) {
203
            // Disassemble key, extracting the first node of the string:
204
            $explodedKey = explode(self::KEY_SEPARATOR, $key);
205
            $currentKey = array_shift($explodedKey);
206
            $newKey = implode(self::KEY_SEPARATOR, $explodedKey);
207
208
            /** @var $object Payone_Config_Abstract */
209
            $object = $tree[$currentKey];
210
            return $object->getValue($newKey);
211
        }
212
        elseif (is_array($tree) and array_key_exists($key, $tree)) {
213
            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...
214
        }
215
        else {
216
            return NULL; // Exit recursion, unsuccessful
217
        }
218
    }
219
220
    /**
221
     * @param Payone_Api_Config $apiConfig
222
     */
223
    public function setApiConfig($apiConfig)
224
    {
225
        $this->apiConfig = $apiConfig;
226
    }
227
228
    /**
229
     * @return Payone_Api_Config
230
     */
231
    public function getApiConfig()
232
    {
233
        return $this->apiConfig;
234
    }
235
236
    /**
237
     * @param Payone_TransactionStatus_Config $transactionStatusConfig
238
     */
239
    public function setTransactionStatusConfig($transactionStatusConfig)
240
    {
241
        $this->transactionStatusConfig = $transactionStatusConfig;
242
    }
243
244
    /**
245
     * @return Payone_TransactionStatus_Config
246
     */
247
    public function getTransactionStatusConfig()
248
    {
249
        return $this->transactionStatusConfig;
250
    }
251
252
    /**
253
     * @param Payone_SessionStatus_Config $sessionStatusConfig
254
     */
255
    public function setSessionStatusConfig($sessionStatusConfig)
256
    {
257
        $this->sessionStatusConfig = $sessionStatusConfig;
258
    }
259
260
    /**
261
     * @return Payone_SessionStatus_Config
262
     */
263
    public function getSessionStatusConfig()
264
    {
265
        return $this->sessionStatusConfig;
266
    }
267
}
268