Completed
Push — master ( 556305...270f06 )
by ARCANEDEV
07:20 queued 07:16
created

CurrencyManager   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 197
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 87.88%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 197
ccs 29
cts 33
cp 0.8788
rs 10
wmc 12
lcom 1
cbo 3

12 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getDefault() 0 4 1
A getDefaultCurrency() 0 4 1
A getSupported() 0 4 1
A currencies() 0 4 1
A load() 0 6 1
A get() 0 4 1
A findOrFail() 0 4 1
A getSupportedCurrencies() 0 9 1
A format() 0 4 1
A symbol() 0 4 1
A isNonIsoIncluded() 0 4 1
1
<?php namespace Arcanedev\Currencies;
2
3
use Arcanedev\Currencies\Contracts\CurrencyManager as CurrencyManagerContract;
4
use Arcanedev\Currencies\Entities\CurrencyCollection;
5
use Illuminate\Support\Arr;
6
7
/**
8
 * Class     CurrencyManager
9
 *
10
 * @package  Arcanedev\Currencies
11
 * @author   ARCANEDEV <[email protected]>
12
 */
13
class CurrencyManager implements CurrencyManagerContract
14
{
15
    /* ------------------------------------------------------------------------------------------------
16
     |  Properties
17
     | ------------------------------------------------------------------------------------------------
18
     */
19
    /**
20
     * Default currency.
21
     *
22
     * @var string
23
     */
24
    protected $default;
25
26
    /**
27
     * Supported currencies.
28
     *
29
     * @var array
30
     */
31
    protected $supported      = [];
32
33
    /**
34
     * Non ISO Currencies included.
35
     *
36
     * @var bool
37
     */
38
    protected $nonIsoIncluded = false;
39
40
    /**
41
     * The currencies collection.
42
     *
43
     * @var  \Arcanedev\Currencies\Entities\CurrencyCollection
44
     */
45
    protected $currencies;
46
47
    /* ------------------------------------------------------------------------------------------------
48
     |  Constructor
49
     | ------------------------------------------------------------------------------------------------
50
     */
51
    /**
52
     * CurrencyManager constructor.
53
     *
54
     * @param  array  $configs
55
     */
56 432
    public function __construct(array $configs)
57
    {
58 432
        $this->default        = Arr::get($configs, 'default', 'USD');
59 432
        $this->supported      = Arr::get($configs, 'supported', ['USD']);
0 ignored issues
show
Documentation Bug introduced by
It seems like \Illuminate\Support\Arr:...pported', array('USD')) of type * is incompatible with the declared type array of property $supported.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
60 432
        $this->nonIsoIncluded = Arr::get($configs, 'include-non-iso', false);
61 432
        $this->currencies     = new CurrencyCollection;
62 432
    }
63
64
    /* ------------------------------------------------------------------------------------------------
65
     |  Getters & Setters
66
     | ------------------------------------------------------------------------------------------------
67
     */
68
    /**
69
     * Get the default currency iso code.
70
     *
71
     * @return string
72
     */
73 12
    public function getDefault()
74
    {
75 12
        return $this->default;
76
    }
77
78
    /**
79
     * Get the default currency entity.
80
     *
81
     * @return \Arcanedev\Currencies\Contracts\Entities\Currency
82
     */
83 12
    public function getDefaultCurrency()
84
    {
85 12
        return $this->currencies->get($this->getDefault());
86
    }
87
88
    /**
89
     * Get supported currencies (iso codes).
90
     *
91
     * @return array
92
     */
93 24
    public function getSupported()
94
    {
95 24
        return $this->supported;
96
    }
97
98
    /**
99
     * Get the currencies collection.
100
     *
101
     * @return \Arcanedev\Currencies\Entities\CurrencyCollection
102
     */
103 432
    public function currencies()
104
    {
105 432
        return $this->currencies;
106
    }
107
108
    /* ------------------------------------------------------------------------------------------------
109
     |  Main Functions
110
     | ------------------------------------------------------------------------------------------------
111
     */
112
    /**
113
     * Load the currencies.
114
     *
115
     * @param  array  $currencies
116
     *
117
     * @return self
118
     */
119 432
    public function load(array $currencies)
120
    {
121 432
        $this->currencies()->load($currencies, $this->nonIsoIncluded);
122
123 432
        return $this;
124
    }
125
126
    /**
127
     * Get a currency from the collection by iso code.
128
     *
129
     * @param  string      $iso
130
     * @param  mixed|null  $default
131
     *
132
     * @return \Arcanedev\Currencies\Entities\Currency
133
     */
134 12
    public function get($iso, $default = null)
135
    {
136 12
        return $this->currencies()->get($iso, $default);
137
    }
138
139
    /**
140
     * Get a currency or fail if not exists.
141
     *
142
     * @param  string  $iso
143
     *
144
     * @return \Arcanedev\Currencies\Entities\Currency
145
     *
146
     * @throws \Arcanedev\Currencies\Exceptions\CurrencyNotFoundException
147
     */
148 36
    public function findOrFail($iso)
149
    {
150 36
        return $this->currencies()->findOrFails($iso);
151
    }
152
153
    /**
154
     * Get the supported currencies collection.
155
     *
156
     * @return \Arcanedev\Currencies\Entities\CurrencyCollection
157
     */
158 12
    public function getSupportedCurrencies()
159
    {
160 12
        $currencies = $this->currencies();
161 12
        $supported  = array_map(function ($iso) use ($currencies) {
162 12
            return $currencies->findOrFails($iso);
163 12
        }, array_combine($this->getSupported(), $this->getSupported()));
164
165 12
        return CurrencyCollection::make($supported);
166
    }
167
168
    /**
169
     * Format the amount.
170
     *
171
     * @param  string  $iso
172
     * @param  int     $amount
173
     * @param  int     $decimals
174
     *
175
     * @return string
176
     *
177
     * @throws \Arcanedev\Currencies\Exceptions\CurrencyNotFoundException
178
     */
179
    public function format($iso, $amount, $decimals = 2)
180
    {
181
        return $this->findOrFail($iso)->format($amount, $decimals);
182
    }
183
184
    /**
185
     * Get the currency symbol by iso code.
186
     *
187
     * @param  string  $iso
188
     *
189
     * @return string
190
     */
191 12
    public function symbol($iso)
192
    {
193 12
        return $this->findOrFail($iso)->symbol;
194
    }
195
196
    /* ------------------------------------------------------------------------------------------------
197
     |  Check Functions
198
     | ------------------------------------------------------------------------------------------------
199
     */
200
    /**
201
     * Check if non ISO Currencies included.
202
     *
203
     * @return bool
204
     */
205
    public function isNonIsoIncluded()
206
    {
207
        return $this->nonIsoIncluded;
208
    }
209
}
210