Manager::resolve()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
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 2
    public function get($name)
26
    {
27 2
        if (!isset($this->formatters[$name])) {
28 2
            $this->formatters[$name] = $this->resolve($name);
29
        }
30 2
        return $this->formatters[$name];
31
    }
32
33
    /**
34
     * @param $name
35
     * @return Repository
36
     */
37 1
    protected function resolve($name)
38
    {
39 1
        return $this->create($name, []);
40
    }
41
42
    /**
43
     * @param $name
44
     * @param array $config
45
     * @return mixed
46
     */
47 1
    protected function create($name, $config = [])
48
    {
49 1
        $driverMethod = 'create' . ucfirst($name) . 'Formatter';
50 1
        if (method_exists($this, $driverMethod)) {
51 1
            return $this->{$driverMethod}($config);
52
        } else {
53
            throw new InvalidArgumentException("Driver [{$name}] is not supported.");
54
        }
55
    }
56
57
    /**
58
     * @param array $config
59
     * @return HtmlFormatter
60
     */
61 1
    protected function createHtmlFormatter($config = [])
62
    {
63 1
        $formatter = new HtmlFormatter(new ISOCurrencies());
64 1
        return $formatter;
65
    }
66
}
67