MapResolver   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 4
dl 0
loc 82
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B setMap() 0 16 5
A getMap() 0 4 1
A resolve() 0 16 3
A collect() 0 4 1
1
<?php
2
3
namespace AssetManager\Core\Resolver;
4
5
use AssetManager\Core\Exception;
6
use Traversable;
7
use Zend\Stdlib\ArrayUtils;
8
9
/**
10
 * This resolver allows you to resolve using a 1 on 1 mapping to a file.
11
 */
12
class MapResolver extends FileResolverAbstract
13
{
14
    /**
15
     * @var array
16
     */
17
    protected $map = array();
18
19
    /**
20
     * Constructor
21
     *
22
     * Instantiate and optionally populate map.
23
     *
24
     * @param array|Traversable $map
25
     */
26
    public function __construct($map = array())
27
    {
28
        $this->setMap($map);
29
    }
30
31
    /**
32
     * Set (overwrite) map
33
     *
34
     * Maps should be arrays or Traversable objects with name => path pairs
35
     *
36
     * @param  array|Traversable                  $map
37
     * @throws Exception\InvalidArgumentException
38
     */
39
    public function setMap($map)
40
    {
41
        if (!is_array($map) && !$map instanceof Traversable) {
42
            throw new Exception\InvalidArgumentException(sprintf(
43
                '%s: expects an array or Traversable, received "%s"',
44
                __METHOD__,
45
                (is_object($map) ? get_class($map) : gettype($map))
46
            ));
47
        }
48
49
        if ($map instanceof Traversable) {
50
            $map = ArrayUtils::iteratorToArray($map);
51
        }
52
53
        $this->map = $map;
54
    }
55
56
    /**
57
     * Retrieve the map
58
     *
59
     * @return array
60
     */
61
    public function getMap()
62
    {
63
        return $this->map;
64
    }
65
66
    /**
67
     * {@inheritDoc}
68
     */
69
    public function resolve($name)
70
    {
71
        if (!isset($this->map[$name])) {
72
            return null;
73
        }
74
75
        $asset = $this->resolveFile($this->map[$name]);
76
77
        if (!$asset) {
78
            return null;
79
        }
80
81
        $asset->mimetype = $this->getMimeResolver()->getMimeType($name);
82
83
        return $asset;
84
    }
85
86
    /**
87
     * {@inheritDoc}
88
     */
89
    public function collect()
90
    {
91
        return array_keys($this->map);
92
    }
93
}
94