Completed
Branch FET-11170-model-use-money-enti... (00bce3)
by
unknown
58:42 queued 45:24
created

CurrencyFactory::createFromCountryCode()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 12

Duplication

Lines 11
Ratio 57.89 %

Importance

Changes 0
Metric Value
cc 3
eloc 12
nc 4
nop 1
dl 11
loc 19
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 EE_Organization_Config;
7
use EEH_File;
8
use EventEspresso\core\domain\values\currency\Currency;
9
use EventEspresso\core\entities\Label;
10
use InvalidArgumentException;
11
12
defined('EVENT_ESPRESSO_VERSION') || exit;
13
14
15
16
/**
17
 * Class CurrencyFactory
18
 * Factory class for creating Currency objects
19
 *
20
 * @package EventEspresso\core\services\currency
21
 * @author  Brent Christensen
22
 * @since   $VID:$
23
 */
24
class CurrencyFactory
25
{
26
27
    /**
28
     * @var array[] $country_currency_data
29
     */
30
    private $country_currency_data;
31
32
    /**
33
     * @var array[] $country_currencies_by_iso_code
34
     */
35
    private $country_currencies_by_iso_code;
36
37
    /**
38
     * @var array[] $country_currencies_by_currency
39
     */
40
    private $country_currencies_by_currency;
41
42
    /**
43
     * @var string $site_country_iso
44
     */
45
    private $site_country_iso;
46
47
48
    /**
49
     * CurrencyFactory constructor.
50
     *
51
     * @param EE_Organization_Config $organization_config
52
     */
53
    public function __construct(EE_Organization_Config  $organization_config) {
54
        $this->site_country_iso = $organization_config->CNT_ISO;
55
    }
56
57
58
    /**
59
     * @return array[]
60
     * @throws EE_Error
61
     */
62
    private function getCountryCurrencyData()
63
    {
64
        if ($this->country_currency_data === null) {
65
            $country_currency_data = json_decode(
66
                EEH_File::get_file_contents(__DIR__ . DS . 'country-currencies.json'),
67
                true
68
            );
69
            $this->parseCountryCurrencyData($country_currency_data);
70
        }
71
        return $this->country_currency_data;
72
    }
73
74
75
    /**
76
     * @param array[] $country_currency_data
77
     */
78
    private function parseCountryCurrencyData($country_currency_data)
79
    {
80
        foreach ($country_currency_data as $country_currency) {
81
            $this->country_currencies_by_iso_code[ $country_currency['CountryISO'] ] = $country_currency;
82
            $this->country_currencies_by_currency[ $country_currency['CurrencyCode'] ] = $country_currency;
83
        }
84
        $this->country_currency_data = $country_currency_data;
85
    }
86
87
88
    /**
89
     * @param array $country_currency
90
     * @return Currency
91
     */
92
    private function createCurrencyFromCountryCurrency(array $country_currency)
93
    {
94
        return new Currency(
95
            $country_currency['CurrencyCode'],
96
            new Label(
97
                $country_currency['CurrencyNameSingle'],
98
                $country_currency['CurrencyNamePlural']
99
            ),
100
            $country_currency['CurrencySign'],
101
            $country_currency['CurrencySignB4'],
102
            $country_currency['CurrencyDecimalPlaces'],
103
            $country_currency['CurrencyDecimalMark'],
104
            $country_currency['CurrencyThousands'],
105
            $country_currency['CurrencySubunits']
106
        );
107
    }
108
109
110
111
    /**
112
     * returns a Currency object for the supplied country code
113
     *
114
     * @param string $CNT_ISO
115
     * @return Currency
116
     * @throws EE_Error
117
     * @throws InvalidArgumentException
118
     */
119
    public function createFromCountryCode($CNT_ISO = null)
120
    {
121
        $this->getCountryCurrencyData();
122
        $CNT_ISO = $CNT_ISO !== null ? $CNT_ISO : $this->site_country_iso;
123 View Code Duplication
        if(! isset($this->country_currencies_by_iso_code[ $CNT_ISO ])) {
124
            throw new InvalidArgumentException(
125
                sprintf(
126
                    esc_html__(
127
                        'Valid country currency data could not be found for the "%1$s" country code;',
128
                        'event_espresso'
129
                    ),
130
                    $CNT_ISO
131
                )
132
            );
133
        }
134
        return $this->createCurrencyFromCountryCurrency(
135
            $this->country_currencies_by_iso_code[ $CNT_ISO ]
136
        );
137
    }
138
139
140
141
    /**
142
     * returns a Currency object for the supplied currency code
143
     * PLZ NOTE: variations may exist between how different countries display the same currency,
144
     * so it may be necessary to use CreateCurrency::fromCountryCode() to achieve the desired results
145
     *
146
     * @param string $code
147
     * @return Currency
148
     * @throws InvalidArgumentException
149
     * @throws EE_Error
150
     */
151
    public function createFromCode($code)
152
    {
153
        $this->getCountryCurrencyData();
154 View Code Duplication
        if (! isset($this->country_currencies_by_currency[ $code ])) {
155
            throw new InvalidArgumentException(
156
                sprintf(
157
                    esc_html__(
158
                        'A valid currency could not be found for the "%1$s" currency code;',
159
                        'event_espresso'
160
                    ),
161
                    $code
162
                )
163
            );
164
        }
165
        return $this->createCurrencyFromCountryCurrency(
166
            $this->country_currencies_by_currency[ $code ]
167
        );
168
    }
169
}
170
// Location: CreateCurrency.php
171