Currencies   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 5
Bugs 0 Features 0
Metric Value
eloc 16
c 5
b 0
f 0
dl 0
loc 66
rs 10
ccs 17
cts 17
cp 1
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A get_currencies() 0 6 2
A get_currency() 0 8 2
A load_currencies() 0 21 2
1
<?php
2
/**
3
 * Currencies
4
 *
5
 * @author    Pronamic <[email protected]>
6
 * @copyright 2005-2023 Pronamic
7
 * @license   GPL-3.0-or-later
8
 * @package   Pronamic\WordPress\Money
9
 */
10
11
namespace Pronamic\WordPress\Money;
12
13
/**
14
 * Currencies
15
 *
16
 * @link https://github.com/moneyphp/money/blob/v3.1.3/resources/currency.php
17
 *
18
 * @author  Remco Tolsma
19
 * @version 2.0.0
20
 * @since   1.0.0
21
 */
22
class Currencies {
23
	/**
24
	 * Map of known currencies indexed by code.
25
	 *
26
	 * @var array<string, Currency>|null
27
	 */
28
	private static $currencies;
29
30
	/**
31
	 * Get currencies.
32
	 *
33
	 * @return array<string, Currency>
34
	 */
35 103
	public static function get_currencies() {
36 103
		if ( is_null( self::$currencies ) ) {
37 1
			self::$currencies = self::load_currencies();
38
		}
39
40 103
		return self::$currencies;
41
	}
42
43
	/**
44
	 * Get currency.
45
	 *
46
	 * @param string $alphabetic_code Alphabetic currency code.
47
	 *
48
	 * @return Currency
49
	 */
50 103
	public static function get_currency( $alphabetic_code ) {
51 103
		$currencies = self::get_currencies();
52
53 103
		if ( \array_key_exists( $alphabetic_code, $currencies ) ) {
54 102
			return $currencies[ $alphabetic_code ];
55
		}
56
57 1
		return new Currency( $alphabetic_code );
58
	}
59
60
	/**
61
	 * Load currencies.
62
	 *
63
	 * @link https://github.com/moneyphp/money/blob/v3.1.3/src/Currencies/ISOCurrencies.php#L90-L102
64
	 * @return array<string, Currency>
65
	 * @throws \RuntimeException Throws runtime exception if currencies could not be loaded from file.
66
	 */
67 1
	private static function load_currencies() {
68 1
		$file = __DIR__ . '/../resources/currencies.php';
69
70 1
		$currencies = [];
71
72
		/**
73
		 * Data.
74
		 *
75
		 * @psalm-suppress UnresolvableInclude
76
		 *
77
		 * @var array<int, Currency>
78
		 */
79 1
		$data = require $file;
80
81 1
		foreach ( $data as $currency ) {
82 1
			$alphabetic_code = $currency->get_alphabetic_code();
83
84 1
			$currencies[ $alphabetic_code ] = $currency;
85
		}
86
87 1
		return $currencies;
88
	}
89
}
90