Currency   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

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

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 11 1
A getFunctions() 0 10 1
A getFilters() 0 6 1
A getCurrencySymbol() 0 10 3
A formatAmount() 0 4 1
A getName() 0 4 1
1
<?php
2
3
namespace DoS\ResourceBundle\Twig\Extension;
4
5
use Sylius\Bundle\MoneyBundle\Templating\Helper\MoneyHelper;
6
use Sylius\Bundle\MoneyBundle\Templating\Helper\MoneyHelperInterface;
7
use Sylius\Bundle\SettingsBundle\Templating\Helper\SettingsHelper;
8
use Symfony\Component\Intl\Intl as SymfonyIntl;
9
use Symfony\Component\Templating\Helper\HelperInterface; #fix small bug in console with Intl naming
10
11
class Currency extends \Twig_Extension
12
{
13
    /**
14
     * @var MoneyHelper
15
     */
16
    protected $moneyHelper;
17
18
    /**
19
     * @var SettingsHelper
20
     */
21
    protected $helper;
22
23
    protected $generalCurrencyKey;
24
25
    protected $currencySymbols = array(
26
        'THB' => '฿',
27
    );
28
29
    public function __construct(
30
        MoneyHelperInterface $moneyHelper,
31
        HelperInterface $helper,
32
        $generalCurrencyKey = 'general.currency',
33
        array $currencySymbols = array()
34
    ) {
35
        $this->moneyHelper = $moneyHelper;
0 ignored issues
show
Documentation Bug introduced by
It seems like $moneyHelper of type object<Sylius\Bundle\Mon...r\MoneyHelperInterface> is incompatible with the declared type object<Sylius\Bundle\Mon...ing\Helper\MoneyHelper> of property $moneyHelper.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
36
        $this->helper = $helper;
0 ignored issues
show
Documentation Bug introduced by
It seems like $helper of type object<Symfony\Component...Helper\HelperInterface> is incompatible with the declared type object<Sylius\Bundle\Set...\Helper\SettingsHelper> of property $helper.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
37
        $this->generalCurrencyKey = $generalCurrencyKey;
38
        $this->currencySymbols = array_merge($this->currencySymbols, $currencySymbols);
39
    }
40
41
    public function getFunctions()
42
    {
43
        $self = array('is_safe' => array('all'));
44
45
        return array(
46
            new \Twig_SimpleFunction(
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFunction has been deprecated with message: to be removed in 3.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
47
                'ui_currency_symbol', array($this, 'getCurrencySymbol'), $self
48
            ),
49
        );
50
    }
51
52
    public function getFilters()
53
    {
54
        return array(
55
            new \Twig_SimpleFilter('ui_money', array($this, 'formatAmount')),
0 ignored issues
show
Deprecated Code introduced by
The class Twig_SimpleFilter has been deprecated with message: to be removed in 3.0

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
56
        );
57
    }
58
59
    public function getCurrencySymbol($currency = null)
60
    {
61
        $currency = $currency ?: $this->helper->getSettingsParameter($this->generalCurrencyKey);
62
63
        if (array_key_exists($currency, $this->currencySymbols)) {
64
            return $this->currencySymbols[$currency];
65
        }
66
67
        return SymfonyIntl::getCurrencyBundle()->getCurrencySymbol($currency);
68
    }
69
70
    /**
71
     * @param      $amount
72
     * @param null $currency
73
     *
74
     * @return string
75
     */
76
    public function formatAmount($amount, $currency = null)
77
    {
78
        return str_replace('.00', '', $this->moneyHelper->formatAmount($amount, $currency));
79
    }
80
81
    /**
82
     * Returns the canonical name of this helper.
83
     *
84
     * @return string The canonical name
85
     *
86
     * @api
87
     */
88
    public function getName()
89
    {
90
        return 'ui_currency';
91
    }
92
}
93