GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

Currency::formatter()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17

Duplication

Lines 17
Ratio 100 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 17
loc 17
ccs 10
cts 10
cp 1
crap 2
rs 9.7
c 0
b 0
f 0
1
<?php
2
3
namespace RafaHernandez\LaravelIntl;
4
5
use CommerceGuys\Intl\Currency\CurrencyRepository;
6
use CommerceGuys\Intl\Formatter\CurrencyFormatter;
7
use CommerceGuys\Intl\NumberFormat\NumberFormatRepository;
8
use Illuminate\Support\Arr;
9
use RafaHernandez\LaravelIntl\Concerns\WithLocales;
10
use RafaHernandez\LaravelIntl\Contracts\Intl;
11
12
class Currency extends Intl
13
{
14
    use WithLocales;
15
16
    /**
17
     * Loaded localized currency data.
18
     *
19
     * @var array
20
     */
21
    protected $data;
22
23
    /**
24
     * Array of localized currency formatters.
25
     *
26
     * @var array
27
     */
28
    protected $formatters;
29
30
    /**
31
     * Alias of get().
32
     *
33
     * @param string $currencyCode
34
     *
35
     * @return string
36
     */
37 12
    public function name($currencyCode)
38
    {
39 12
        return $this->get($currencyCode);
40
    }
41
42
    /**
43
     * Get a localized record by key.
44
     *
45
     * @param string $currencyCode
46
     *
47
     * @return string
48
     */
49 15
    public function get($currencyCode)
50
    {
51 15
        return $this->data()->get($currencyCode)->getName();
52
    }
53
54
    /**
55
     * The currency repository.
56
     *
57
     * @return \CommerceGuys\Intl\Currency\CurrencyRepository
58
     */
59 33 View Code Duplication
    protected function data()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
60
    {
61 33
        $key = $this->getLocalesKey(
62 33
            $locale = $this->getLocale(),
63 33
            $fallbackLocale = $this->getFallbackLocale()
64
        );
65
66 33
        if (!isset($this->data[$key])) {
67 33
            $this->data[$key] = new CurrencyRepository($locale, $fallbackLocale);
68
        }
69
70 33
        return $this->data[$key];
71
    }
72
73
    /**
74
     * Get the formatter's key.
75
     *
76
     * @param string $locale
77
     * @param string $fallbackLocale
78
     *
79
     * @return string
80
     */
81 33
    protected function getLocalesKey($locale, $fallbackLocale)
82
    {
83 33
        return implode('|', [
84 33
            $locale,
85 33
            $fallbackLocale,
86
        ]);
87
    }
88
89
    /**
90
     * Get the symbol of the given currency.
91
     *
92
     * @param string $currencyCode
93
     *
94
     * @return string
95
     */
96 3
    public function symbol($currencyCode)
97
    {
98 3
        return $this->data()->get($currencyCode)->getSymbol();
99
    }
100
101
    /**
102
     * Format a number.
103
     *
104
     * @param string|int|float $number
105
     * @param string           $currencyCode
106
     * @param array            $options
107
     *
108
     * @return mixed|string
109
     */
110 12
    public function format($number, $currencyCode, $options = [])
111
    {
112 12
        return $this->formatter()->format($number, $currencyCode,
113 12
            $this->mergeOptions($options)
114
        );
115
    }
116
117
    /**
118
     * The current number formatter.
119
     *
120
     * @return \CommerceGuys\Intl\Formatter\CurrencyFormatter
121
     */
122 18 View Code Duplication
    protected function formatter()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
123
    {
124 18
        $key = $this->getLocalesKey(
125 18
            $locale = $this->getLocale(),
126 18
            $fallbackLocale = $this->getFallbackLocale()
127
        );
128
129 18
        if (!isset($this->formatters[$key])) {
130 18
            $this->formatters[$key] = new CurrencyFormatter(
131 18
                new NumberFormatRepository($fallbackLocale),
132 18
                $this->data(),
133 18
                ['locale' => $locale]
134
            );
135
        }
136
137 18
        return $this->formatters[$key];
138
    }
139
140
    /**
141
     * Merges the options array.
142
     *
143
     * @param array $options
144
     * @param array $defaults
145
     *
146
     * @return array
147
     */
148 18
    protected function mergeOptions(array $options, array $defaults = [])
149
    {
150 18
        Arr::forget($options, 'locale');
151
152 18
        return $defaults + $options;
153
    }
154
155
    /**
156
     * Format a number.
157
     *
158
     * @param string|int|float $number
159
     * @param string           $currencyCode
160
     * @param array            $options
161
     *
162
     * @return mixed|string
163
     */
164 3
    public function formatAccounting($number, $currencyCode, $options = [])
165
    {
166 3
        return $this->formatter()->format($number, $currencyCode,
167 3
            $this->mergeOptions($options, ['style' => 'accounting'])
168
        );
169
    }
170
171
    /**
172
     * Parse a localized currency string into a number.
173
     *
174
     * @param string $number
175
     * @param string $currencyCode
176
     * @param array  $options
177
     *
178
     * @return mixed|string
179
     */
180 3
    public function parse($number, $currencyCode, $options = [])
181
    {
182 3
        return $this->formatter()->parse($number, $currencyCode,
183 3
            $this->mergeOptions($options)
184
        );
185
    }
186
187
    /**
188
     * Get all localized records.
189
     *
190
     * @return array
191
     */
192 3
    public function all()
193
    {
194 3
        return $this->data()->getList();
195
    }
196
}
197