Currency::isActive()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 1
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
namespace Fomvasss\Currency;
4
5
use Illuminate\Support\Arr;
6
7
class Currency
8
{
9
    protected $config;
10
11
    protected $symbol = null;
12
13
    protected $userCurrency;
14
15
    /**
16
     * Currency constructor.
17
     */
18
    public function __construct(array $config = null)
19
    {
20
        if (!$config) {
21
            $config = config('currency', []);
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

21
            $config = /** @scrutinizer ignore-call */ config('currency', []);
Loading history...
22
        }
23
24
        $this->config = $config;
25
    }
26
27
    /**
28
     * @param $value
29
     * @param null $code
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $code is correct as it would always require null to be passed?
Loading history...
30
     * @param null $symbol
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $symbol is correct as it would always require null to be passed?
Loading history...
31
     * @return string
32
     */
33
    public function format($value, $code = null, $symbol = null)
34
    {
35
        $code = $this->prepareCode($code);
36
37
        $precision = $this->config("currencies.$code.precision", 0);
38
        $decimalSeparator = $this->config("currencies.$code.decimalSeparator", "");
39
        $thousandSeparator = $this->config("currencies.$code.thousandSeparator", "");
40
41
        $symbol = $symbol ?? $this->config("currencies.$code.symbol", "");
42
43
        $value = $this->prepareValue($value, $code);
44
45
        $result = number_format($value, $precision, $decimalSeparator, $thousandSeparator);
46
47
        if ($symbol) {
48
            return $this->config("currencies.$code.symbolPlacement") == 'after' ? $result.$symbol : $symbol.$result;
0 ignored issues
show
Bug introduced by
Are you sure $symbol of type Illuminate\Config\Repository|array|mixed can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

48
            return $this->config("currencies.$code.symbolPlacement") == 'after' ? $result./** @scrutinizer ignore-type */ $symbol : $symbol.$result;
Loading history...
49
        }
50
51
        return $result;
52
    }
53
54
    /**
55
     * @param $amount
56
     * @param null $from
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $from is correct as it would always require null to be passed?
Loading history...
57
     * @param null $to
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $to is correct as it would always require null to be passed?
Loading history...
58
     * @param bool $format
59
     * @return float|int|string|null
60
     */
61
    public function convert($amount, $from = null, $to = null, $format = true)
62
    {
63
        // Get currencies involved
64
        $from = $from ?: $this->config('default');
0 ignored issues
show
introduced by
$from is of type null, thus it always evaluated to false.
Loading history...
65
        $to = $to ?: $this->getUserCurrency();
0 ignored issues
show
introduced by
$to is of type null, thus it always evaluated to false.
Loading history...
66
67
        // Get exchange rates
68
        $fromRate = $this->getCurrencyProp($from, 'exchangeRate');
0 ignored issues
show
Bug introduced by
It seems like $from can also be of type array; however, parameter $code of Fomvasss\Currency\Currency::getCurrencyProp() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

68
        $fromRate = $this->getCurrencyProp(/** @scrutinizer ignore-type */ $from, 'exchangeRate');
Loading history...
69
        $toRate = $this->getCurrencyProp($to, 'exchangeRate');
70
71
        // Skip invalid to currency rates
72
        if ($toRate === null) {
0 ignored issues
show
introduced by
The condition $toRate === null is always false.
Loading history...
73
            return null;
74
        }
75
        // Convert amount
76
        if ($from === $to) {
77
            $value = $amount;
78
        } else {
79
            $value = ($amount * $toRate) / $fromRate;
80
        }
81
        // Should the result be formatted?
82
        if ($format === true) {
83
            return $this->format($value, $to);
84
        }
85
86
        // Return value
87
        return $this->prepareValue($value, $to);
88
    }
89
90
    protected function prepareValue($value, $code)
91
    {
92
        if ($this->config("divide_result") && ($coin = $this->config("currencies.$code.coin", 0))) {
93
            $value = $value / $coin;
94
        }
95
96
        return $value;
97
    }
98
99
    /**
100
     * Get the given property value from provided currency.
101
     *
102
     * @param string $code
103
     * @param string $key
104
     * @param mixed  $default
105
     *
106
     * @return array
107
     */
108
    protected function getCurrencyProp($code, $key, $default = null)
109
    {
110
        return Arr::get($this->getCurrency($code), $key, $default);
111
    }
112
113
    /**
114
     * Return all currencies.
115
     *
116
     * @return array
117
     */
118
    public function getCurrencies()
119
    {
120
        return $this->config('currencies', []);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->config('currencies', array()) also could return the type Illuminate\Config\Repository which is incompatible with the documented return type array.
Loading history...
121
    }
122
123
    /**
124
     *
125
     * Determine if the provided currency is valid.
126
     *
127
     * @param $code
128
     * @return bool
129
     */
130
    public function issetCurrency($code)
131
    {
132
        return array_key_exists(strtoupper($code), $this->getCurrencies());
133
    }
134
135
    /**
136
     *
137
     * Determine if the provided currency is active.
138
     *
139
     * @param $code
140
     * @return bool
141
     */
142
    public function isActive($code)
143
    {
144
        return $code && (bool) Arr::get($this->getCurrency($code), 'active', false);
145
    }
146
147
    /**
148
     * Return the current currency.
149
     *
150
     * @param null $code
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $code is correct as it would always require null to be passed?
Loading history...
151
     * @return mixed
152
     */
153
    public function getCurrency($code = null)
154
    {
155
        $code = $code ?: $this->getUserCurrency();
0 ignored issues
show
introduced by
$code is of type null, thus it always evaluated to false.
Loading history...
156
157
        return Arr::get($this->getCurrencies(), strtoupper($code));
0 ignored issues
show
Bug introduced by
It seems like $code can also be of type array; however, parameter $string of strtoupper() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

157
        return Arr::get($this->getCurrencies(), strtoupper(/** @scrutinizer ignore-type */ $code));
Loading history...
158
    }
159
160
    public function setUserCurrency($code)
161
    {
162
        $this->userCurrency = strtoupper($code);
163
164
        return $this;
165
    }
166
167
    /**
168
     * @return array|\Illuminate\Config\Repository|mixed
0 ignored issues
show
Bug introduced by
The type Illuminate\Config\Repository was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
169
     */
170
    public function getUserCurrency()
171
    {
172
        return $this->userCurrency ?? $this->config('default');
173
    }
174
175
    /**
176
     * Return all active currencies.
177
     *
178
     * @return array
179
     */
180
    public function getActiveCurrencies()
181
    {
182
        return array_filter($this->getCurrencies(), function($currency) {
183
            return $currency['active'] == true;
184
        });
185
    }
186
187
    /**
188
     * Return currencies config.
189
     *
190
     * @param null $key
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $key is correct as it would always require null to be passed?
Loading history...
191
     * @param null $default
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $default is correct as it would always require null to be passed?
Loading history...
192
     * @return array|\Illuminate\Config\Repository|mixed
193
     */
194
    protected function config($key = null, $default = null)
195
    {
196
        if ($key === null) {
0 ignored issues
show
introduced by
The condition $key === null is always true.
Loading history...
197
            return $this->config;
198
        }
199
        return Arr::get($this->config, $key, $default);
200
    }
201
202
    /**
203
     * @param null $code
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $code is correct as it would always require null to be passed?
Loading history...
204
     * @return null|string
205
     */
206
    protected function prepareCode($code = null)
207
    {
208
        if (! $code) {
0 ignored issues
show
introduced by
$code is of type null, thus it always evaluated to false.
Loading history...
209
            $code =  $this->config("default", 'USD');
210
        }
211
212
        if (array_key_exists($code, $this->getCurrencies())) {
213
            return $code;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $code also could return the type array which is incompatible with the documented return type null|string.
Loading history...
214
        }
215
216
        return 'USD';
217
    }
218
}