ResolverCache::loadCache()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 2
eloc 2
c 1
b 0
f 1
nc 2
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
1
<?php
2
3
namespace Nip\Dispatcher\Resolver\Cache;
4
5
use Nip\Utility\Traits\SingletonTrait;
6
7
/**
8
 * Class ResolverCache
9
 * @package Nip\Dispatcher\Resolver\Cache
10
 */
11
class ResolverCache
12
{
13
    use SingletonTrait;
14
15
    /**
16
     * @var DefinitionsCollection
17
     */
18
    protected $definitions;
19
20
    /**
21
     * ResolverCache constructor.
22
     * @param DefinitionsCollection $definitions
23
     */
24 1
    protected function __construct(DefinitionsCollection $definitions = null)
25
    {
26 1
        $definitions = $definitions instanceof DefinitionsCollection ? $definitions : new DefinitionsCollection([]);
27 1
        $this->definitions = $definitions;
28 1
        $this->loadCache();
29 1
    }
30
31
    /**
32
     * @param array $action
33
     * @return string
34
     */
35 3
    public static function keyFromAction($action)
36
    {
37 3
        $module = isset($action['module']) ? $action['module'] : '';
38 3
        $controller = isset($action['controller']) ? $action['controller'] : '';
39 3
        return $module . '-' . $controller;
40
    }
41
42
    /**
43
     * @param array $action
44
     * @return mixed|null
45
     */
46 3
    public static function resolveFromAction($action)
47
    {
48 3
        $key = static::keyFromAction($action);
49 3
        return static::resolveFromKey($key);
50
    }
51
52
    /**
53
     * @param array $action
54
     * @param $class
55
     * @return mixed|null
56
     */
57 3
    public static function setFromAction($action, $class)
58
    {
59 3
        $resolver = static::instance();
60 3
        $key = static::keyFromAction($action);
61 3
        $resolver->definitions->set($key, $class);
62 3
    }
63
64
    /**
65
     * @param array $action
66
     * @param $class
67
     * @return mixed|null
68
     */
69 2
    public static function save()
70
    {
71 2
        $resolver = static::instance();
72 2
        FileManager::write($resolver->definitions);
73 2
    }
74
75
    /**
76
     * @param $key
77
     * @return mixed|null
78
     */
79 3
    public static function resolveFromKey($key)
80
    {
81 3
        $resolver = static::instance();
82 3
        if ($resolver->definitions->has($key)) {
83 1
            return $resolver->definitions->get($key);
84
        }
85 2
        return null;
86
    }
87
88 1
    protected function loadCache()
89
    {
90 1
        if (FileManager::hasCacheFile()) {
91 1
            FileManager::read($this->definitions);
92
        }
93 1
    }
94
}
95