Completed
Push — master ( dda000...a904dd )
by Daniel
02:11
created

ForeignCurrency::manageCurrencyToDisplay()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 3
eloc 7
nc 3
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
    private $currencyDetails;
35
36
    private function getCurrencyExchangeRates(\XMLReader $xml)
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 manageCurrencyToDisplay(\Symfony\Component\HttpFoundation\Request $tCSG)
53
    {
54
        $aCurrencyDetails = [];
55
        $xMoneyChosen     = array_merge(['RON'], $tCSG->request->get('xMoney'));
56
        foreach ($this->currencyDetails['CX'] as $key2 => $value2) {
57
            if (in_array($key2, $xMoneyChosen)) {
58
                $aCurrencyDetails[$key2] = $value2;
59
            }
60
        }
61
        return $aCurrencyDetails;
62
    }
63
64
    private function setCurrencyExchangeVariables($aryRelevantCrncy, $kCX)
65
    {
66
        $this->currencyDetails = [
67
            'CX'  => $aryRelevantCrncy,
68
            'CXD' => strtotime('now'),
69
        ];
70
        foreach ($kCX as $value) {
71
            $this->currencyDetails['CXV'][$value] = 1;
72
        }
73
    }
74
75
    private function setExchangeRateValues($appSettings, $aryRelevantCrncy)
76
    {
77
        $kCX = array_keys($aryRelevantCrncy);
78
        $this->setCurrencyExchangeVariables($aryRelevantCrncy, $kCX);
79
        $this->updateCurrencyExchangeRatesFile($appSettings);
80
        $xml = new \XMLReader();
81
        if ($xml->open($appSettings['Exchange Rate Local'], 'UTF-8')) {
82
            while ($xml->read()) {
83
                if ($xml->nodeType == \XMLReader::ELEMENT) {
84
                    $this->getCurrencyExchangeRates($xml);
85
                }
86
            }
87
            $xml->close();
88
        }
89
    }
90
91
    private function updateCurrencyExchangeRatesFile($appSettings)
92
    {
93
        if ((filemtime($appSettings['Exchange Rate Local']) + 90 * 24 * 60 * 60) < time()) {
94
            $fCntnt = file_get_contents($appSettings['Exchange Rate Source']);
95
            if ($fCntnt !== false) {
96
                file_put_contents($appSettings['Exchange Rate Local'], $fCntnt);
97
                chmod($appSettings['Exchange Rate Local'], 0666);
98
            }
99
        }
100
    }
101
}
102