Completed
Push — master ( 56570e...2c8793 )
by Gabriel
07:17
created

ResolverCache   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 3
dl 0
loc 84
ccs 30
cts 30
cp 1
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A keyFromAction() 0 6 3
A resolveFromAction() 0 5 1
A setFromAction() 0 6 1
A save() 0 5 1
A resolveFromKey() 0 8 2
A loadCache() 0 6 2
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
0 ignored issues
show
Bug introduced by
There is no parameter named $action. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
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