Completed
Push — master ( 28f264...c80ec8 )
by Daniel
06:10
created

ForeignCurrency::setCurrencyExchangeVariables()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 6
nc 2
nop 1
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)
37
    {
38
        switch ($xml->localName) {
39
            case 'Cube':
40
                $this->currencyDetails['CXD'] = strtotime($xml->getAttribute('date'));
41
                break;
42
            case 'Rate':
43
                if (in_array($xml->getAttribute('currency'), $aryRelevantCurrencies)) {
44
                    $multiplier = 1;
45
                    if (!is_null($xml->getAttribute('multiplier'))) {
46
                        $multiplier = $xml->getAttribute('multiplier');
47
                    }
48
                    $this->currencyDetails['CXV'][$xml->getAttribute('currency')] = $xml->readInnerXml() / $multiplier;
49
                }
50
                break;
51
        }
52
    }
53
54
    private function setCurrencyExchangeVariables($aryRelevantCurrencies)
55
    {
56
        $this->currencyDetails = [
57
            'CX'  => $aryRelevantCurrencies,
58
            'CXD' => strtotime('now'),
59
        ];
60
        foreach (array_keys($aryRelevantCurrencies) as $value) {
61
            $this->currencyDetails['CXV'][$value] = 1;
62
        }
63
    }
64
65
    protected function setExchangeRateValues($appSettings, $aryRelevantCurrencies)
66
    {
67
        $this->setCurrencyExchangeVariables($aryRelevantCurrencies);
68
        //$this->updateCurrencyExchangeRatesFile($appSettings);
0 ignored issues
show
Unused Code Comprehensibility introduced by
86% 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...
69
        $xml = new \XMLReader();
70
        if ($xml->open($appSettings['Exchange Rate Local'], 'UTF-8')) {
71
            while ($xml->read()) {
72
                if ($xml->nodeType == \XMLReader::ELEMENT) {
73
                    $this->getCurrencyExchangeRates($xml, array_keys($aryRelevantCurrencies));
74
                }
75
            }
76
            $xml->close();
77
        }
78
    }
79
80
    private function updateCurrencyExchangeRatesFile($appSettings)
0 ignored issues
show
Unused Code introduced by
This method is not used, and could be removed.
Loading history...
81
    {
82
        if ((filemtime($appSettings['Exchange Rate Local']) + 90 * 24 * 60 * 60) < time()) {
83
            $fCntnt = file_get_contents($appSettings['Exchange Rate Source']);
84
            if ($fCntnt !== false) {
85
                file_put_contents($appSettings['Exchange Rate Local'], $fCntnt);
86
                chmod($appSettings['Exchange Rate Local'], 0666);
87
            }
88
        }
89
    }
90
}
91