Completed
Push — master ( 48514f...802925 )
by Ivo
02:35
created

MoneyFormatter::formatI18nDecimal()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 12
Ratio 100 %

Code Coverage

Tests 6
CRAP Score 2.0625

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 12
loc 12
ccs 6
cts 8
cp 0.75
rs 9.4285
cc 2
eloc 7
nc 2
nop 2
crap 2.0625
1
<?php
2
3
namespace Ivoba\Money\Formatter;
4
5
use SebastianBergmann\Money\Money;
6
use Symfony\Component\Intl\Intl;
7
8
class MoneyFormatter
9
{
10
    /**
11
     * @var
12
     */
13
    private $locale;
14
15
    /**
16
     * @param null|string $locale
17
     */
18 18
    public function __construct($locale = null)
19
    {
20 18
        $this->locale = $locale;
21 18
        if (is_null($this->locale)) {
22 6
            $this->locale = \Locale::getDefault();
23 6
        }
24 18
    }
25
26
    /**
27
     * localized format for money with the NumberFormatter class in Decimal mode
28
     * without currency symbol
29
     * like 1.234,56
30
     *
31
     * @param Money $money
32
     * @param null  $locale
33
     * @return bool|string
34
     */
35 3 View Code Duplication
    public function formatI18nDecimal(Money $money, $locale = null)
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...
36
    {
37 3
        if ($locale === null) {
38
            $locale = $this->locale;
39
        }
40 3
        $formatter = new \NumberFormatter(
41 3
            $locale,
42
            \NumberFormatter::DECIMAL
43 3
        );
44
45 3
        return $formatter->format($money->getConvertedAmount());
46
    }
47
48
    /**
49
     * localized format for money with the NumberFormatter class in Currency mode
50
     * with currency symbol
51
     * like €1.234,56
52
     *
53
     * @param Money $money
54
     * @param null  $locale
55
     * @return bool|string
56
     */
57 9 View Code Duplication
    public function formatI18n(Money $money, $locale = null)
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...
58
    {
59 9
        if ($locale === null) {
60 3
            $locale = $this->locale;
61 3
        }
62
63 9
        $formatter = new \NumberFormatter(
64 9
            $locale,
65
            \NumberFormatter::CURRENCY
66 9
        );
67
68 9
        return $formatter->formatCurrency(
69 9
            $money->getConvertedAmount(),
70 9
            $money->getCurrency()->getCurrencyCode()
71 9
        );
72
    }
73
74
    /**
75
     * gets currency symbol
76
     *
77
     * @param Money $money
78
     * @return null|string
79
     */
80 6
    public function formatCurrencyAsSymbol(Money $money)
81
    {
82 6
        return Intl::getCurrencyBundle()->getCurrencySymbol($money->getCurrency()->getCurrencyCode());
83
    }
84
85
    /**
86
     * gets currency name
87
     *
88
     * @param Money $money
89
     * @return null|string
90
     */
91 6
    public function formatCurrencyAsName(Money $money)
92
    {
93 6
        return Intl::getCurrencyBundle()->getCurrencyName($money->getCurrency()->getCurrencyCode());
94
    }
95
}
96