Completed
Push — master ( f906b5...0ffad1 )
by Kamil
17s
created

CurrencyNameConverter   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 19
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 2
c 1
b 0
f 1
lcom 0
cbo 2
dl 0
loc 19
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A convertToCode() 0 13 2
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sylius\Component\Currency\Converter;
13
14
use Symfony\Component\Intl\Intl;
15
16
/**
17
 * @author Anna Walasek <[email protected]>
18
 */
19
class CurrencyNameConverter implements CurrencyNameConverterInterface
20
{
21
    /**
22
     * {@inheritdoc}
23
     */
24
    public function convertToCode($name, $locale = 'en')
25
    {
26
        $names = Intl::getCurrencyBundle()->getCurrencyNames($locale);
27
        $currencyCode = array_search($name, $names, true);
28
29
        if (false === $currencyCode) {
30
            throw new \InvalidArgumentException(sprintf(
31
                'Currency "%s" not found! Available names: %s.', $name, implode(', ', $names)
32
            ));
33
        }
34
35
        return $currencyCode;
36
    }
37
}
38