Test Failed
Push — master ( 646e1f...19314b )
by Gabriel
02:47
created

Manager   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 48
rs 10
wmc 6

4 Methods

Rating   Name   Duplication   Size   Complexity  
A resolve() 0 3 1
A get() 0 4 2
A create() 0 7 2
A createHtmlFormatter() 0 4 1
1
<?php
2
3
namespace ByTIC\Money\Formatter;
4
5
use InvalidArgumentException;
6
use Money\Currencies\ISOCurrencies;
7
use Money\MoneyFormatter;
8
use Nip\Cache\Stores\Repository;
9
use Nip\Utility\Traits\SingletonTrait;
10
11
/**
12
 * Class Manager
13
 * @package ByTIC\Money\Formatter
14
 */
15
class Manager
16
{
17
    use SingletonTrait;
18
19
    protected $formatters = [];
20
21
    /**
22
     * @param $name
23
     * @return MoneyFormatter
24
     */
25
    public function get($name)
26
    {
27
        $this->formatters[$name] = isset($this->formatters[$name]) ?: $this->resolve($name);
28
        return $this->formatters[$name];
29
    }
30
31
    /**
32
     * @param $name
33
     * @return Repository
34
     */
35
    protected function resolve($name)
36
    {
37
        return $this->create($name, []);
38
    }
39
40
    /**
41
     * @param $name
42
     * @param array $config
43
     * @return mixed
44
     */
45
    protected function create($name, $config = [])
46
    {
47
        $driverMethod = 'create' . ucfirst($name) . 'Formatter';
48
        if (method_exists($this, $driverMethod)) {
49
            return $this->{$driverMethod}($config);
50
        } else {
51
            throw new InvalidArgumentException("Driver [{$name}] is not supported.");
52
        }
53
    }
54
55
    /**
56
     * @param array $config
57
     * @return HtmlFormatter
58
     */
59
    protected function createHtmlFormatter($config = [])
60
    {
61
        $formatter = new HtmlFormatter(new ISOCurrencies());
62
        return $formatter;
63
    }
64
}
65