PriceExtension   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 28
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getFilters() 0 6 1
A formatCurrency() 0 11 2
1
<?php
2
3
namespace AppBundle\Price\Twig;
4
5
use AppBundle\Price\Price;
6
7
class PriceExtension extends \Twig_Extension
8
{
9
    private $numberFormatter;
10
11
    public function __construct($locale = 'fr_FR', $style = \NumberFormatter::CURRENCY)
12
    {
13
        $this->numberFormatter = new \NumberFormatter($locale, $style);
14
    }
15
16
    public function getFilters()
17
    {
18
        return array(
19
            new \Twig_SimpleFilter('format_currency', array($this, 'formatCurrency'))
20
        );
21
    }
22
23
    public function formatCurrency($price)
24
    {
25
        if (is_array($price)) {
26
            $price = new Price($price[0], $price[1]);
27
        }
28
29
        $amount = $price->getAmount();
30
        $currency = $price->getCurrency();
31
32
        return $this->numberFormatter->formatCurrency($amount, $currency);
33
    }
34
}
35