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

Manager::get()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 4
rs 10
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