AggregateResolver::attach()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
namespace AssetManager\Core\Resolver;
4
5
use Zend\Stdlib\PriorityQueue;
6
7
/**
8
 * The aggregate resolver consists out of a multitude of
9
 * resolvers defined by the ResolverInterface.
10
 */
11
class AggregateResolver implements ResolverInterface
12
{
13
    /**
14
     * @var PriorityQueue|ResolverInterface[]
15
     */
16
    protected $queue;
17
18
    /**
19
     * Constructor
20
     *
21
     * Instantiate the internal priority queue
22
     */
23
    public function __construct()
24
    {
25
        $this->queue = new PriorityQueue();
26
    }
27
28
    /**
29
     * Attach a resolver
30
     *
31
     * @param  ResolverInterface $resolver
32
     * @param  int               $priority
33
     * @return self
34
     */
35
    public function attach(ResolverInterface $resolver, $priority = 1)
36
    {
37
        $this->queue->insert($resolver, $priority);
38
        return $this;
39
    }
40
41
    /**
42
     * {@inheritDoc}
43
     */
44
    public function resolve($name)
45
    {
46
        foreach ($this->queue as $resolver) {
47
            $resource = $resolver->resolve($name);
48
            if (null !== $resource) {
49
                return $resource;
50
            }
51
        }
52
53
        return null;
54
    }
55
56
    /**
57
     * {@inheritDoc}
58
     */
59
    public function collect()
60
    {
61
        $collection = array();
62
63
        foreach ($this->queue as $resolver) {
64
            if (!method_exists($resolver, 'collect')) {
65
                continue;
66
            }
67
68
            $collection = array_merge($resolver->collect(), $collection);
69
        }
70
71
        return array_unique($collection);
72
    }
73
}
74