PrivateCollectionResolver   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 105
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Importance

Changes 0
Metric Value
wmc 11
lcom 2
cbo 6
dl 0
loc 105
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B resolve() 0 26 4
A getCollectionAsset() 0 20 3
A setAssetFilterManager() 0 4 1
A getAssetFilterManager() 0 4 1
A collect() 0 4 1
1
<?php
2
3
namespace AssetManager\Core\Resolver;
4
5
use Assetic\Asset\AssetCollection;
6
use AssetManager\Core\Exception;
7
use AssetManager\Core\Service\AssetFilterManager;
8
use AssetManager\Core\Service\AssetFilterManagerAwareInterface;
9
use Traversable;
10
11
/**
12
 * This resolver allows the resolving of collections with no addition mapping.
13
 * Unlike the collection resolver, the assets mapped here can not
14
 * be accessed outside the requested filename.
15
 */
16
class PrivateCollectionResolver extends FileResolverAbstract implements AssetFilterManagerAwareInterface
17
{
18
    use CollectionTrait;
19
20
    /**
21
     * @var AssetFilterManager The filterManager service.
22
     */
23
    protected $filterManager;
24
25
    /**
26
     * Constructor
27
     *
28
     * Instantiate and optionally populate collections.
29
     *
30
     * @param array|Traversable $collections
31
     */
32
    public function __construct($collections = array())
33
    {
34
        $this->setCollections($collections);
35
    }
36
37
    /**
38
     * {@inheritDoc}
39
     */
40
    public function resolve($name)
41
    {
42
        $collectionMap = $this->getCollections();
43
44
        if (!isset($collectionMap[$name])) {
45
            return null;
46
        }
47
48
        if (!is_array($collectionMap[$name])) {
49
            throw new Exception\RuntimeException(
50
                "Collection with name $name is not an an array."
51
            );
52
        }
53
54
        $collection = new AssetCollection;
55
56
        $collection->setTargetPath($name);
57
58
        foreach ($collectionMap[$name] as $path) {
59
            $collection->add($this->getCollectionAsset($path));
60
        }
61
62
        $collection->mimetype = $this->getMimeResolver()->getMimeType($name);
0 ignored issues
show
Bug introduced by
The property mimetype does not seem to exist in Assetic\Asset\AssetCollection.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
63
64
        return $collection;
65
    }
66
67
    /**
68
     * @param string $path
69
     *
70
     * @return \Assetic\Asset\FileAsset|\Assetic\Asset\HttpAsset|null
71
     */
72
    public function getCollectionAsset($path)
73
    {
74
        if (!is_string($path)) {
75
            throw new Exception\RuntimeException(
76
                'Asset should be of type string. got ' . gettype($path)
77
            );
78
        }
79
80
        $asset = $this->resolveFile($path);
81
82
        if (empty($asset)) {
83
            throw new Exception\RuntimeException(
84
                'Unable to locate file: '.$path
85
            );
86
        }
87
88
        $this->getAssetFilterManager()->setFilters($path, $asset);
89
90
        return $asset;
91
    }
92
93
    /**
94
     * Set the AssetFilterManager.
95
     *
96
     * @param AssetFilterManager $filterManager
97
     */
98
    public function setAssetFilterManager(AssetFilterManager $filterManager)
99
    {
100
        $this->filterManager = $filterManager;
101
    }
102
103
    /**
104
     * Get the AssetFilterManager
105
     *
106
     * @return AssetFilterManager
107
     */
108
    public function getAssetFilterManager()
109
    {
110
        return $this->filterManager;
111
    }
112
113
    /**
114
     * {@inheritDoc}
115
     */
116
    public function collect()
117
    {
118
        return array_keys($this->collections);
119
    }
120
}
121