CollectionResolver::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace AssetManager\Core\Resolver;
4
5
use Assetic\Asset\AssetCollection;
6
use Assetic\Asset\AssetInterface;
7
use AssetManager\Core\Exception;
8
use AssetManager\Core\Service\AssetFilterManager;
9
use AssetManager\Core\Service\AssetFilterManagerAwareInterface;
10
use Traversable;
11
12
/**
13
 * This resolver allows the resolving of collections.
14
 * Collections are strictly checked by mime-type,
15
 * and added to an AssetCollection when all checks passed.
16
 */
17
class CollectionResolver implements
18
    ResolverInterface,
19
    AggregateResolverAwareInterface,
20
    AssetFilterManagerAwareInterface
21
{
22
    use CollectionTrait;
23
24
    /**
25
     * @var ResolverInterface
26
     */
27
    protected $aggregateResolver;
28
29
    /**
30
     * @var AssetFilterManager The filterManager service.
31
     */
32
    protected $filterManager;
33
34
    /**
35
     * Constructor
36
     *
37
     * Instantiate and optionally populate collections.
38
     *
39
     * @param array|Traversable $collections
40
     */
41
    public function __construct($collections = array())
42
    {
43
        $this->setCollections($collections);
44
    }
45
46
    /**
47
     * Set the aggregate resolver.
48
     *
49
     * @param ResolverInterface $aggregateResolver
50
     */
51
    public function setAggregateResolver(ResolverInterface $aggregateResolver)
52
    {
53
        $this->aggregateResolver = $aggregateResolver;
54
    }
55
56
    /**
57
     * Get the aggregate resolver.
58
     *
59
     * @return ResolverInterface
60
     */
61
    public function getAggregateResolver()
62
    {
63
        return $this->aggregateResolver;
64
    }
65
66
    /**
67
     * {@inheritDoc}
68
     */
69
    public function resolve($name)
70
    {
71
        $collections = $this->getCollections();
72
73
        if (!isset($collections[$name])) {
74
            return null;
75
        }
76
77
        if (!is_array($collections[$name])) {
78
            throw new Exception\RuntimeException(
79
                "Collection with name $name is not an an array."
80
            );
81
        }
82
83
        $collection = new AssetCollection;
84
        $mimeType   = null;
85
        $collection->setTargetPath($name);
86
87
        foreach ($collections[$name] as $asset) {
88
            if (!is_string($asset)) {
89
                throw new Exception\RuntimeException(
90
                    'Asset should be of type string. got ' . gettype($asset)
91
                );
92
            }
93
94
            if (null === ($res = $this->getAggregateResolver()->resolve($asset))) {
95
                throw new Exception\RuntimeException("Asset '$asset' could not be found.");
96
            }
97
98
            if (!$res instanceof AssetInterface) {
99
                throw new Exception\RuntimeException(
100
                    "Asset '$asset' does not implement Assetic\\Asset\\AssetInterface."
101
                );
102
            }
103
104
            if (null !== $mimeType && $res->mimetype !== $mimeType) {
0 ignored issues
show
Bug introduced by
Accessing mimetype on the interface Assetic\Asset\AssetInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
105
                throw new Exception\RuntimeException(sprintf(
106
                    'Asset "%s" from collection "%s" doesn\'t have the expected mime-type "%s".',
107
                    $asset,
108
                    $name,
109
                    $mimeType
110
                ));
111
            }
112
113
            $mimeType = $res->mimetype;
0 ignored issues
show
Bug introduced by
Accessing mimetype on the interface Assetic\Asset\AssetInterface suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
114
115
            $this->getAssetFilterManager()->setFilters($asset, $res);
116
117
            $collection->add($res);
118
        }
119
120
        $collection->mimetype = $mimeType;
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...
121
122
        return $collection;
123
    }
124
125
    /**
126
     * Set the AssetFilterManager.
127
     *
128
     * @param AssetFilterManager $filterManager
129
     */
130
    public function setAssetFilterManager(AssetFilterManager $filterManager)
131
    {
132
        $this->filterManager = $filterManager;
133
    }
134
135
    /**
136
     * Get the AssetFilterManager
137
     *
138
     * @return AssetFilterManager
139
     */
140
    public function getAssetFilterManager()
141
    {
142
        return $this->filterManager;
143
    }
144
145
    /**
146
     * {@inheritDoc}
147
     */
148
    public function collect()
149
    {
150
        return array_keys($this->collections);
151
    }
152
}
153