Payone_Api_Mapper_Currency::getMappingByCode()   A
last analyzed

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 6
nc 4
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
 * @category        Payone
16
 * @package         Payone_Api
17
 * @subpackage      Mapper
18
 * @copyright       Copyright (c) 2012 <[email protected]> - www.noovias.com
19
 * @author          Matthias Walter <[email protected]>
20
 * @license         <http://www.gnu.org/licenses/> GNU General Public License (GPL 3)
21
 * @link            http://www.noovias.com
22
 */
23
24
/**
25
 *
26
 * @category        Payone
27
 * @package         Payone_Api
28
 * @subpackage      Mapper
29
 * @copyright       Copyright (c) 2012 <[email protected]> - www.noovias.com
30
 * @license         <http://www.gnu.org/licenses/> GNU General Public License (GPL 3)
31
 * @link            http://www.noovias.com
32
 */
33
class Payone_Api_Mapper_Currency extends Payone_Api_Mapper_Abstract
34
    implements Payone_Api_Mapper_Currency_Interface
35
{
36
    const PATH_CURRENCY_PROPERTIES_DEFAULT = 'currency.properties';
37
38
    /** Property names from properties file */
39
    const PROPERTY_NAME_ID = 'id';
40
    const PROPERTY_NAME_SIGN = 'sign';
41
    const PROPERTY_NAME_CODE = 'code';
42
    const PROPERTY_NAME_SUBDIV = 'subdiv';
43
44
    /**
45
     * @var string
46
     */
47
    protected $pathToProperties = '';
48
    /**
49
     * @var array
50
     */
51
    protected $mappingByCode = null;
52
    /**
53
     * @var array
54
     */
55
    protected $mappingById = null;
56
57
    /**
58
     * @param $amount
59
     * @param $code
60
     * @return string
61
     *
62
     */
63
    public function mapAmountToSub($amount, $code)
64
    {
65
        $subdiv = $this->initSubdivByCode($code);
66
        return number_format($amount * $subdiv, 0, '.', '');
67
    }
68
69
    /**
70
     * @param $subAmount
71
     * @param string $code
72
     * @return string
73
     * @throws Payone_Api_Exception_MappingNotFound
74
     */
75
    public function mapAmountToMain($subAmount, $code)
76
    {
77
        $subdiv = $this->initSubdivByCode($code);
78
        return number_format((float)$subAmount / $subdiv, 2, '.', '');
79
    }
80
81
    /**
82
     * @param string $code
83
     * @return string
84
     * @throws Payone_Api_Exception_MappingNotFound
85
     */
86 View Code Duplication
    public function getIdByCode($code)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
87
    {
88
        $code = strtolower($code);
89
        if (!$this->hasMappingByCode($code, self::PROPERTY_NAME_ID)) {
90
            throw new Payone_Api_Exception_MappingNotFound();
91
        }
92
93
        $mapping = $this->getMappingByCode($code);
94
        return $mapping[self::PROPERTY_NAME_ID];
95
    }
96
97
    /**
98
     * @param $id
99
     * @return string
100
     * @throws Payone_Api_Exception_MappingNotFound
101
     */
102
    public function getCodeById($id)
103
    {
104
        $id = trim((string)$id);
105
        $mappingById = $this->getMappingById();
106
        if (!array_key_exists($id, $mappingById) || trim($mappingById[$id]) === '') {
107
            throw new Payone_Api_Exception_MappingNotFound();
108
        }
109
110
        return $mappingById[$id];
111
    }
112
113
    /**
114
     * @param string $code
115
     * @return string
116
     * @throws Payone_Api_Exception_MappingNotFound
117
     */
118 View Code Duplication
    public function getCurrencySymbolByCode($code)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
119
    {
120
        $code = strtolower($code);
121
        if (!$this->hasMappingByCode($code, self::PROPERTY_NAME_SIGN)) {
122
            throw new Payone_Api_Exception_MappingNotFound();
123
        }
124
125
        $mapping = $this->getMappingByCode($code);
126
        return $mapping[self::PROPERTY_NAME_SIGN];
127
    }
128
129
    /**
130
     * @param $code
131
     * @return int
132
     * @throws Payone_Api_Exception_MappingNotFound
133
     */
134
    protected function initSubdivByCode($code)
135
    {
136
        $code = strtolower($code);
137
        if (!$this->hasMappingByCode($code, self::PROPERTY_NAME_SUBDIV)) {
138
            throw new Payone_Api_Exception_MappingNotFound();
139
        }
140
141
        $mapping = $this->getMappingByCode($code);
142
143
        $subdiv = $mapping[self::PROPERTY_NAME_SUBDIV];
144
        if (!is_numeric($subdiv)) {
145
            throw new Payone_Api_Exception_MappingNotFound();
146
        }
147
148
        $subdiv = (int)$subdiv;
149
        if ($subdiv === 0) {
150
            $subdiv = 1;
151
        }
152
153
        return $subdiv;
154
    }
155
156
    /**
157
     * @return array
158
     */
159
    protected function initCurrenciesRaw()
160
    {
161
        $pathToProperties = $this->getPathToProperties();
162
        $pathToProperties = $pathToProperties != '' ? $pathToProperties : self::PATH_CURRENCY_PROPERTIES_DEFAULT;
163
164
        $contents = file_get_contents($pathToProperties, true);
165
        $lines = explode(PHP_EOL, $contents);
166
        $currenciesRaw = array();
167
        foreach ($lines as $key => $line) {
168
            $lines[$key] = trim($line);
169
            if ($lines[$key] !== '') {
170
                $exploded = explode('=', $lines[$key]);
171
                if (count($exploded) !== 2) {
172
                    continue;
173
                }
174
175
                $key = trim($exploded[0]);
176
                $value = trim($exploded[1]);
177
                $currenciesRaw[$key] = $value;
178
            }
179
        }
180
181
        return $currenciesRaw;
182
    }
183
184
    /**
185
     * @return array
186
     */
187
    protected function initMappingByCode()
188
    {
189
        $currenciesRaw = $this->initCurrenciesRaw();
190
191
        $mappingByCode = array();
192
        foreach ($currenciesRaw as $key => $data) {
193
            $explodedKey = explode('.', $key);
194
            if (count($explodedKey) != 3) {
195
                continue;
196
            }
197
198
            $type = $explodedKey[1];
199
            $typeCode = $explodedKey[2];
200
201
            /** ignore the assignment of id to code and type should be set */
202
            if (is_numeric($typeCode) or $type == '') {
203
                continue;
204
            }
205
206
            $typeCodeKey = $type;
207
            if ($type == self::PROPERTY_NAME_CODE) {
208
                $typeCodeKey = self::PROPERTY_NAME_ID;
209
            }
210
211
            if ($typeCodeKey == '') {
212
                continue;
213
            }
214
215
            if (!array_key_exists($typeCode, $mappingByCode)) {
216
                $mappingByCode[$typeCode] = array();
217
            }
218
219
            $mappingByCode[$typeCode][$typeCodeKey] = trim($data);
220
        }
221
222
        ksort($mappingByCode);
223
        return $mappingByCode;
224
    }
225
226
    /**
227
     * @param array $mappingByCode
228
     */
229
    public function setMappingByCode(array $mappingByCode)
230
    {
231
        $this->mappingByCode = $mappingByCode;
232
    }
233
234
    /**
235
     * @param string $code
236
     * @return array
237
     */
238
    public function getMappingByCode($code = '')
239
    {
240
        if ($this->mappingByCode === null) {
241
            $this->mappingByCode = $this->initMappingByCode();
242
        }
243
244
        if ($code != '') {
245
            return $this->mappingByCode[$code];
246
        }
247
248
        return $this->mappingByCode;
249
    }
250
251
    /**
252
     * @param $code
253
     * @param $innerArrayKey ('id' | 'sign' | 'subdiv')
254
     * @return bool
255
     */
256
    public function hasMappingByCode($code, $innerArrayKey)
257
    {
258
        $mappingByCode = $this->getMappingByCode();
259
        if (!array_key_exists($code, $mappingByCode)) {
260
            return false;
261
        }
262
263
        if (!array_key_exists($innerArrayKey, $mappingByCode[$code])) {
264
            return false;
265
        }
266
267
        if ($mappingByCode[$code][$innerArrayKey] === '') {
268
            return false;
269
        }
270
271
        return true;
272
    }
273
274
    /**
275
     * @return array
276
     *
277
     * mappingById is derived from mappingByCode
278
     */
279
    public function getMappingById()
280
    {
281
        if ($this->mappingById === null) {
282
            $this->mappingById = $this->initMappingById();
283
        }
284
285
        return $this->mappingById;
286
    }
287
288
    /**
289
     * @return array
290
     */
291
    protected function initMappingById()
292
    {
293
        $mappingByCode = $this->getMappingByCode();
294
        $mappingById = array();
295
        foreach ($mappingByCode as $code => $data) {
296
            if (!array_key_exists(self::PROPERTY_NAME_ID, $data)) {
297
                continue;
298
            }
299
300
            $id = $data[self::PROPERTY_NAME_ID];
301
            if ($id !== '') {
302
                $mappingById[$id] = $code;
303
            }
304
        }
305
306
        return $mappingById;
307
    }
308
309
    /**
310
     * @param string $pathToProperties
311
     */
312
    public function setPathToProperties($pathToProperties)
313
    {
314
        $this->pathToProperties = $pathToProperties;
315
    }
316
317
    /**
318
     * @return string
319
     */
320
    public function getPathToProperties()
321
    {
322
        return $this->pathToProperties;
323
    }
324
}
325