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

MoneyFactory   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 163
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 163
rs 10
c 1
b 0
f 0
wmc 12
lcom 1
cbo 6

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A createForSite() 0 8 1
A createForCountry() 0 8 1
A createForCurrency() 0 8 1
A calculator() 0 5 1
A createFromSubUnits() 0 10 1
B initializeCalculators() 0 22 6
1
<?php
2
3
namespace EventEspresso\core\services\currency;
4
5
use EE_Error;
6
use EventEspresso\core\domain\values\currency\Money;
7
use EventEspresso\core\exceptions\InvalidDataTypeException;
8
use EventEspresso\core\exceptions\InvalidIdentifierException;
9
use EventEspresso\core\exceptions\InvalidInterfaceException;
10
use InvalidArgumentException;
11
12
defined('EVENT_ESPRESSO_VERSION') || exit;
13
14
15
16
/**
17
 * Class MoneyFactory
18
 * Factory class for creating Money objects
19
 *
20
 * @package       Event Espresso
21
 * @author        Brent Christensen
22
 * @since         $VID:$
23
 */
24
class MoneyFactory
25
{
26
27
    /**
28
     * @var CurrencyFactory $currency_factory
29
     */
30
    protected $currency_factory;
31
32
    /**
33
     * @var Calculator $calculator
34
     */
35
    protected $calculator;
36
37
38
    /**
39
     * CreateMoney constructor.
40
     *
41
     * @param CurrencyFactory  $currency_factory
42
     * @param Calculator       $calculator
43
     */
44
    public function __construct(
45
        CurrencyFactory $currency_factory,
46
        Calculator $calculator = null
47
    ) {
48
        $this->calculator = $calculator;
49
        $this->currency_factory = $currency_factory;
50
    }
51
52
53
    /**
54
     * factory method that returns a Money object using amount specified in the currency's subunits
55
     * example: for $12.50 USD use CreateMoney::fromSubUnits(1250, 'USD')
56
     *
57
     * @param int    $subunits_amount money amount IN THE SUBUNITS FOR THE CURRENCY ie: cents
58
     *                                example: $12.50 USD would equate to a subunits amount of 1250
59
     * @param string $currency_code
60
     * @return Money
61
     * @throws EE_Error
62
     * @throws InvalidArgumentException
63
     * @throws InvalidDataTypeException
64
     */
65
    public function createFromSubUnits($subunits_amount, $currency_code = '')
66
    {
67
        $currency = $this->currency_factory->createFromCode($currency_code);
68
        return new Money(
69
            // shift decimal BACK by number of places for currency
70
            $subunits_amount * pow(10, $currency->decimalPlaces() * -1),
71
            $currency,
72
            $this->calculator()
73
        );
74
    }
75
76
77
78
    /**
79
     * factory method that returns a Money object using the currency corresponding to the site's country
80
     * example: CreateMoney::forSite(12.5)
81
     *
82
     * @param float|int|string $amount money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc
83
     *                                 example: $12.5 USD would equate to a standard amount of 12.50
84
     * @return Money
85
     * @throws EE_Error
86
     * @throws InvalidArgumentException
87
     * @throws InvalidDataTypeException
88
     */
89
    public function createForSite($amount)
90
    {
91
        return new Money(
92
            $amount,
93
            $this->currency_factory->createFromCountryCode(\EE_Config::instance()->organization->CNT_ISO),
94
            $this->calculator()
95
        );
96
    }
97
98
99
100
    /**
101
     * factory method that returns a Money object using the currency as specified by the supplied ISO country code
102
     * example: CreateMoney::forCountry(12.5,'US')
103
     *
104
     * @param float|int|string $amount money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc
105
     *                                 example: $12.5 USD would equate to a value amount of 12.50
106
     * @param string           $CNT_ISO
107
     * @return Money
108
     * @throws EE_Error
109
     * @throws InvalidArgumentException
110
     * @throws InvalidDataTypeException
111
     */
112
    public function createForCountry($amount, $CNT_ISO)
113
    {
114
        return new Money(
115
            $amount,
116
            $this->currency_factory->createFromCountryCode($CNT_ISO),
117
            $this->calculator()
118
        );
119
    }
120
121
122
123
    /**
124
     * factory method that returns a Money object using the currency as specified by the supplied currency code
125
     * example: CreateMoney::forCurrency(12.5, 'USD')
126
     *
127
     * @param float|int|string $amount money amount IN THE STANDARD UNIT FOR THE CURRENCY ie: dollars, Euros, etc
128
     *                                 example: $12.5 USD would equate to a value amount of 12.50
129
     * @param string           $currency_code
130
     * @return Money
131
     * @throws EE_Error
132
     * @throws InvalidArgumentException
133
     * @throws InvalidDataTypeException
134
     */
135
    public function createForCurrency($amount, $currency_code)
136
    {
137
        return new Money(
138
            $amount,
139
            $this->currency_factory->createFromCode($currency_code),
140
            $this->calculator()
141
        );
142
    }
143
144
145
146
147
    /**
148
     * @return Calculator
149
     */
150
    public function calculator()
151
    {
152
        $this->initializeCalculators();
153
        return $this->calculator;
154
    }
155
156
157
158
    /**
159
     * loops through a filterable array of Calculator services
160
     * and selects the first one that is supported by the current server
161
     */
162
    protected function initializeCalculators()
163
    {
164
        if ($this->calculator instanceof Calculator) {
165
            return;
166
        }
167
        $calculators = apply_filters(
168
            'FHEE__EventEspresso_core_services_currency_MoneyFactory__initializeCalculators__Calculators_array',
169
            array(
170
                'EventEspresso\core\services\currency\DefaultCalculator',
171
            )
172
        );
173
        foreach ($calculators as $calculator) {
174
            if (! class_exists($calculator)) {
175
                continue;
176
            }
177
            $calculator = new $calculator();
178
            if ($calculator instanceof Calculator && $calculator->isSupported()) {
179
                $this->calculator = $calculator;
180
                break;
181
            }
182
        }
183
    }
184
185
186
}
187
// End of file CreateMoney.php
188
// Location: core/services/currency/CreateMoney.php
189