Completed
Branch FET/11129/facilitate-stripe-an... (fc6ced)
by
unknown
90:22 queued 79:00
created

initializeCountryCurrencyData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 7
nc 2
nop 0
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace EventEspresso\core\services\currency;
4
5
use EE_Error;
6
use EEH_File;
7
use InvalidArgumentException;
8
9
defined('EVENT_ESPRESSO_VERSION') || exit;
10
11
12
13
/**
14
 * Class CountryCurrencyDao
15
 * Description
16
 *
17
 * @package EventEspresso\core\services\currency
18
 * @author  Brent Christensen
19
 * @since   $VID:$
20
 */
21
class CountryCurrencyDao
22
{
23
24
    /**
25
     * @var array[] $country_currency_data
26
     */
27
    private $country_currency_data;
28
29
    /**
30
     * @var array[] $country_currencies_by_iso_code
31
     */
32
    private $country_currencies_by_iso_code;
33
34
    /**
35
     * @var array[] $country_currencies_by_currency
36
     */
37
    private $country_currencies_by_currency;
38
39
40
    /**
41
     * @return array[]
42
     * @throws EE_Error
43
     */
44
    private function initializeCountryCurrencyData()
45
    {
46
        if ($this->country_currency_data === null) {
47
            $country_currency_data = json_decode(
48
                EEH_File::get_file_contents(__DIR__ . DS . 'country-currencies.json'),
49
                true
50
            );
51
            $this->parseCountryCurrencyData($country_currency_data);
52
        }
53
        return $this->country_currency_data;
54
    }
55
56
57
    /**
58
     * @param array[] $country_currency_data
59
     */
60
    private function parseCountryCurrencyData($country_currency_data)
61
    {
62
        foreach ($country_currency_data as $country_currency) {
63
            $this->country_currencies_by_iso_code[ $country_currency['CountryISO'] ]   = $country_currency;
64
            $this->country_currencies_by_currency[ $country_currency['CurrencyCode'] ] = $country_currency;
65
        }
66
        $this->country_currency_data = $country_currency_data;
67
    }
68
69
70
    /**
71
     * @param string $CNT_ISO
72
     * @return array
73
     * @throws EE_Error
74
     * @throws InvalidArgumentException
75
     */
76 View Code Duplication
    public function getCountryCurrencyByIsoCode($CNT_ISO = '')
77
    {
78
        $this->initializeCountryCurrencyData();
79
        if (! isset($this->country_currencies_by_iso_code[ $CNT_ISO ])) {
80
            throw new InvalidArgumentException(
81
                sprintf(
82
                    esc_html__(
83
                        'Valid country currency data could not be found for the "%1$s" country code;',
84
                        'event_espresso'
85
                    ),
86
                    $CNT_ISO
87
                )
88
            );
89
        }
90
        return $this->country_currencies_by_iso_code[ $CNT_ISO ];
91
    }
92
93
94
    /**
95
     * @param string $code
96
     * @return array
97
     * @throws EE_Error
98
     * @throws InvalidArgumentException
99
     */
100 View Code Duplication
    public function getCountryCurrencyByCurrencyCode($code = '')
101
    {
102
        $this->initializeCountryCurrencyData();
103
        if (! isset($this->country_currencies_by_currency[ $code ])) {
104
            throw new InvalidArgumentException(
105
                sprintf(
106
                    esc_html__(
107
                        'A valid currency could not be found for the "%1$s" currency code;',
108
                        'event_espresso'
109
                    ),
110
                    $code
111
                )
112
            );
113
        }
114
        return $this->country_currencies_by_currency[ $code ];
115
    }
116
}
117