MapsManager::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
declare(strict_types = 1);
3
4
namespace PTS\DataTransformer;
5
6
use Symfony\Component\Yaml\Parser as YamlParser;
7
8
class MapsManager
9
{
10
    /** @var YamlParser */
11
    protected $yamlParser;
12
    /** @var array */
13
    protected $cache = [];
14
    /** @var array */
15
    protected $mapsDirs = [];
16
17 13
    public function __construct(YamlParser $parser)
18
    {
19 13
        $this->yamlParser = $parser;
20 13
    }
21
22 9
    public function setMapDir(string $name, string $dir)
23
    {
24 9
        $this->mapsDirs[$name] = $dir;
25 9
    }
26
27 9
    public function getMap(string $name, string $type = 'dto'): array
28
    {
29 9
        $map = $this->tryCache($name, $type);
30 9
        if (is_array($map)) {
31 2
            return $map;
32
        }
33
34 9
        $dir = $this->mapsDirs[$name];
35 9
        $map = $this->getByPath($dir . '/' . $type . '.yml');
36
37 9
        $this->setCache($name, $type, $map);
38 9
        return $map;
39
    }
40
41 9
    protected function setCache(string $name, string $type, array $map)
42
    {
43 9
        $this->cache[$name][$type] = $map;
44 9
    }
45
46 9
    protected function tryCache(string $name, string $type): ?array
47
    {
48 9
        if (isset($this->cache[$name], $this->cache[$name][$type])) {
49 2
            return $this->cache[$name][$type];
50
        }
51
52 9
        return null;
53
    }
54
55 9
    protected function getByPath(string $path): array
56
    {
57 9
        return (array)$this->yamlParser->parse(file_get_contents($path));
58
    }
59
}
60