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

CurrencyFactory   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 0
loc 83
rs 10
c 0
b 0
f 0
wmc 5
lcom 1
cbo 4

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A createCurrencyFromCountryCurrency() 0 16 1
A createFromCountryCode() 0 7 2
A createFromCode() 0 6 1
1
<?php
2
3
namespace EventEspresso\core\services\currency;
4
5
use EE_Error;
6
use EE_Organization_Config;
7
use EventEspresso\core\domain\values\currency\Currency;
8
use EventEspresso\core\entities\Label;
9
use InvalidArgumentException;
10
11
defined('EVENT_ESPRESSO_VERSION') || exit;
12
13
14
15
/**
16
 * Class CurrencyFactory
17
 * Factory class for creating Currency objects
18
 *
19
 * @package EventEspresso\core\services\currency
20
 * @author  Brent Christensen
21
 * @since   $VID:$
22
 */
23
class CurrencyFactory
24
{
25
26
    /**
27
     * @var string $site_country_iso
28
     */
29
    private $site_country_iso;
30
31
    /**
32
     * @var CountryCurrencyDao $country_currencies
33
     */
34
    private $country_currencies;
35
36
37
    /**
38
     * CurrencyFactory constructor.
39
     *
40
     * @param CountryCurrencyDao     $country_currencies
41
     * @param EE_Organization_Config $organization_config
42
     */
43
    public function __construct(CountryCurrencyDao $country_currencies, EE_Organization_Config $organization_config)
44
    {
45
        $this->country_currencies = $country_currencies;
46
        $this->site_country_iso   = $organization_config->CNT_ISO;
47
    }
48
49
50
    /**
51
     * @param array $country_currency
52
     * @return Currency
53
     */
54
    private function createCurrencyFromCountryCurrency(array $country_currency)
55
    {
56
        return new Currency(
57
            $country_currency['CurrencyCode'],
58
            new Label(
59
                $country_currency['CurrencyNameSingle'],
60
                $country_currency['CurrencyNamePlural']
61
            ),
62
            $country_currency['CurrencySign'],
63
            $country_currency['CurrencySignB4'],
64
            $country_currency['CurrencyDecimalPlaces'],
65
            $country_currency['CurrencyDecimalMark'],
66
            $country_currency['CurrencyThousands'],
67
            $country_currency['CurrencySubunits']
68
        );
69
    }
70
71
72
    /**
73
     * returns a Currency object for the supplied country code
74
     *
75
     * @param string $CNT_ISO
76
     * @return Currency
77
     * @throws EE_Error
78
     * @throws InvalidArgumentException
79
     */
80
    public function createFromCountryCode($CNT_ISO = null)
81
    {
82
        $CNT_ISO = $CNT_ISO !== null ? $CNT_ISO : $this->site_country_iso;
83
        return $this->createCurrencyFromCountryCurrency(
84
            $this->country_currencies->getCountryCurrencyByIsoCode($CNT_ISO)
85
        );
86
    }
87
88
89
    /**
90
     * returns a Currency object for the supplied currency code
91
     * PLZ NOTE: variations may exist between how different countries display the same currency,
92
     * so it may be necessary to use CreateCurrency::fromCountryCode() to achieve the desired results
93
     *
94
     * @param string $code
95
     * @return Currency
96
     * @throws InvalidArgumentException
97
     * @throws EE_Error
98
     */
99
    public function createFromCode($code)
100
    {
101
        return $this->createCurrencyFromCountryCurrency(
102
            $this->country_currencies->getCountryCurrencyByCurrencyCode($code)
103
        );
104
    }
105
}
106
// Location: CreateCurrency.php
107