Completed
Push — master ( c80ec8...da2a08 )
by Daniel
02:43
created

ForeignCurrency::getCurrencyExchangeRates()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 15
rs 9.2
cc 4
eloc 11
nc 4
nop 2
1
<?php
2
3
/**
4
 *
5
 * The MIT License (MIT)
6
 *
7
 * Copyright (c) 2016 Daniel Popiniuc
8
 *
9
 * Permission is hereby granted, free of charge, to any person obtaining a copy
10
 * of this software and associated documentation files (the "Software"), to deal
11
 * in the Software without restriction, including without limitation the rights
12
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
 * copies of the Software, and to permit persons to whom the Software is
14
 * furnished to do so, subject to the following conditions:
15
 *
16
 * The above copyright notice and this permission notice shall be included in all
17
 * copies or substantial portions of the Software.
18
 *
19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
 *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25
 * SOFTWARE.
26
 *
27
 */
28
29
namespace danielgp\salariu;
30
31
trait ForeignCurrency
32
{
33
34
    protected $currencyDetails;
35
36
    private function getCurrencyExchangeRates($xml, $aryRelevantCurrencies)
0 ignored issues
show
Unused Code introduced by
The parameter $aryRelevantCurrencies is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
37
    {
38
        switch ($xml->localName) {
39
            case 'Cube':
40
                $this->currencyDetails['CXD'] = strtotime($xml->getAttribute('date'));
41
                break;
42
            case 'Rate':
43
                $multiplier                   = 1;
44
                if (!is_null($xml->getAttribute('multiplier'))) {
45
                    $multiplier = $xml->getAttribute('multiplier');
46
                }
47
                $this->currencyDetails['CXV'][$xml->getAttribute('currency')] = $xml->readInnerXml() / $multiplier;
48
                break;
49
        }
50
    }
51
52
    private function setCurrencyExchangeVariables($aryRelevantCurrencies)
53
    {
54
        $this->currencyDetails = [
55
            'CX'  => $aryRelevantCurrencies,
56
            'CXD' => strtotime('now'),
57
        ];
58
        foreach (array_keys($aryRelevantCurrencies) as $value) {
59
            $this->currencyDetails['CXV'][$value] = 1;
60
        }
61
    }
62
63
    protected function setExchangeRateValues($appSettings, $aryRelevantCurrencies)
64
    {
65
        $this->setCurrencyExchangeVariables($aryRelevantCurrencies);
66
        $this->updateCurrencyExchangeRatesFile($appSettings);
67
        $xml = new \XMLReader();
68
        if ($xml->open($appSettings['Exchange Rate Local'], 'UTF-8')) {
69
            while ($xml->read()) {
70
                if ($xml->nodeType == \XMLReader::ELEMENT) {
71
                    $this->getCurrencyExchangeRates($xml, array_keys($aryRelevantCurrencies));
72
                }
73
            }
74
            $xml->close();
75
        }
76
    }
77
78
    private function updateCurrencyExchangeRatesFile($appSettings)
79
    {
80
        if ((filemtime($appSettings['Exchange Rate Local']) + 90 * 24 * 60 * 60) < time()) {
81
            $fCntnt = file_get_contents($appSettings['Exchange Rate Source']);
82
            if ($fCntnt !== false) {
83
                file_put_contents($appSettings['Exchange Rate Local'], $fCntnt);
84
                chmod($appSettings['Exchange Rate Local'], 0666);
85
            }
86
        }
87
    }
88
}
89