Test Failed
Push — release/4.x ( 1432ca...131f10 )
by Erik
05:53
created

DelegatingProvider   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 96
rs 10
c 0
b 0
f 0
wmc 18

7 Methods

Rating   Name   Duplication   Size   Complexity  
A url() 0 14 4
A suggest() 0 9 3
A all() 0 9 3
A supports() 0 8 3
A addProvider() 0 4 1
A getProviders() 0 5 3
A __construct() 0 3 1
1
<?php
2
/**
3
 * @author Gerard van Helden <[email protected]>
4
 * @copyright Zicht Online <http://zicht.nl>
5
 */
6
7
namespace Zicht\Bundle\UrlBundle\Url;
8
9
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
10
use Zicht\Bundle\UrlBundle\Exception\UnsupportedException;
11
12
/**
13
 * A provider that delegates to a number of registered providers, ordered by priority.
14
 */
15
class DelegatingProvider implements Provider, SuggestableProvider, ListableProvider
16
{
17
    /**
18
     * @var Provider[]
19
     */
20
    protected $providers;
21
22
    /**
23
     * Initialize the provider
24
     */
25
    public function __construct()
26
    {
27
        $this->providers = [];
28
    }
29
30
    /**
31
     * Add a provider with the specified priority. Higher priority means exactly that ;)
32
     *
33
     * @param Provider $provider
34
     * @param int $priority
35
     * @return void
36
     */
37
    public function addProvider(Provider $provider, $priority = 0)
38
    {
39
        $this->providers[$priority][] = $provider;
40
        ksort($this->providers);
41
     }
42
43
    /**
44
     * {@inheritDoc}
45
     */
46
    public function supports($object)
47
    {
48
        foreach ($this->getProviders() as $provider) {
49
            if ($provider->supports($object)) {
50
                return true;
51
            }
52
        }
53
        return false;
54
    }
55
56
    /**
57
     * {@inheritDoc}
58
     */
59
    public function url($object, array $options = array())
60
    {
61
        foreach ($this->getProviders() as $provider) {
62
            if ($provider->supports($object)) {
63
                return $provider->url($object, $options);
64
            }
65
        }
66
67
68
        $objectType = is_object($object)
69
            ? get_class($object)
70
            : (gettype($object) . ' (' . var_export($object, true) . ')');
71
72
        throw new UnsupportedException("Can not render url for {$objectType}");
73
    }
74
75
    /**
76
     * {@inheritDoc}
77
     */
78
    public function suggest($pattern)
79
    {
80
        $ret = array();
81
        foreach ($this->getProviders() as $provider) {
82
            if ($provider instanceof SuggestableProvider) {
83
                $ret = array_merge($ret, $provider->suggest($pattern));
84
            }
85
        }
86
        return $ret;
87
    }
88
89
    /**
90
     * {@inheritDoc}
91
     */
92
    public function all(AuthorizationCheckerInterface $securityContext)
93
    {
94
        $ret = array();
95
        foreach ($this->getProviders() as $provider) {
96
            if ($provider instanceof ListableProvider) {
97
                $ret = array_merge($ret, $provider->all($securityContext));
98
            }
99
        }
100
        return $ret;
101
    }
102
103
    /**
104
     * @return Provider[]|\Generator
105
     */
106
    private function getProviders()
107
    {
108
        foreach ($this->providers as $providers) {
109
            foreach ($providers as $provider) {
110
                yield $provider;
111
            }
112
        }
113
    }
114
}
115