Completed
Branch BUG/11288/fix-datepicker (d15367)
by
unknown
108:07 queued 94:31
created

CountryCurrencyDao   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 96
Duplicated Lines 33.33 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 32
loc 96
rs 10
c 0
b 0
f 0
wmc 8
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A initializeCountryCurrencyData() 0 11 2
A parseCountryCurrencyData() 0 8 2
A getCountryCurrencyByIsoCode() 16 16 2
A getCountryCurrencyByCurrencyCode() 16 16 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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
 * retrieves country currency data from the country-currencies.json
16
 * in order to provide an accurate and uneditable data source
17
 * For user configurable currency details, use EE_Currency_Config
18
 *
19
 * @package EventEspresso\core\services\currency
20
 * @author  Brent Christensen
21
 * @since   $VID:$
22
 */
23
class CountryCurrencyDao
24
{
25
26
    /**
27
     * @var array[] $country_currency_data
28
     */
29
    private $country_currency_data;
30
31
    /**
32
     * @var array[] $country_currencies_by_iso_code
33
     */
34
    private $country_currencies_by_iso_code;
35
36
    /**
37
     * @var array[] $country_currencies_by_currency
38
     */
39
    private $country_currencies_by_currency;
40
41
42
    /**
43
     * @return array[]
44
     * @throws EE_Error
45
     */
46
    private function initializeCountryCurrencyData()
47
    {
48
        if ($this->country_currency_data === null) {
49
            $country_currency_data = json_decode(
50
                EEH_File::get_file_contents(__DIR__ . DS . 'country-currencies.json'),
51
                true
52
            );
53
            $this->parseCountryCurrencyData($country_currency_data);
54
        }
55
        return $this->country_currency_data;
56
    }
57
58
59
    /**
60
     * @param array[] $country_currency_data
61
     */
62
    private function parseCountryCurrencyData($country_currency_data)
63
    {
64
        foreach ($country_currency_data as $country_currency) {
65
            $this->country_currencies_by_iso_code[ $country_currency['CountryISO'] ]   = $country_currency;
66
            $this->country_currencies_by_currency[ $country_currency['CurrencyCode'] ] = $country_currency;
67
        }
68
        $this->country_currency_data = $country_currency_data;
69
    }
70
71
72
    /**
73
     * @param string $CNT_ISO
74
     * @return array
75
     * @throws EE_Error
76
     * @throws InvalidArgumentException
77
     */
78 View Code Duplication
    public function getCountryCurrencyByIsoCode($CNT_ISO = '')
79
    {
80
        $this->initializeCountryCurrencyData();
81
        if (! isset($this->country_currencies_by_iso_code[ $CNT_ISO ])) {
82
            throw new InvalidArgumentException(
83
                sprintf(
84
                    esc_html__(
85
                        'Valid country currency data could not be found for the "%1$s" country code;',
86
                        'event_espresso'
87
                    ),
88
                    $CNT_ISO
89
                )
90
            );
91
        }
92
        return $this->country_currencies_by_iso_code[ $CNT_ISO ];
93
    }
94
95
96
    /**
97
     * @param string $code
98
     * @return array
99
     * @throws EE_Error
100
     * @throws InvalidArgumentException
101
     */
102 View Code Duplication
    public function getCountryCurrencyByCurrencyCode($code = '')
103
    {
104
        $this->initializeCountryCurrencyData();
105
        if (! isset($this->country_currencies_by_currency[ $code ])) {
106
            throw new InvalidArgumentException(
107
                sprintf(
108
                    esc_html__(
109
                        'A valid currency could not be found for the "%1$s" currency code;',
110
                        'event_espresso'
111
                    ),
112
                    $code
113
                )
114
            );
115
        }
116
        return $this->country_currencies_by_currency[ $code ];
117
    }
118
}
119