Passed
Push — up-php7 ( a2fe11...4e1c1e )
by Erik
05:48
created

DelegatingProvider::addProvider()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
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
use function Zicht\Itertools\iterable;
12
13
/**
14
 * A provider that delegates to a number of registered providers, ordered by priority.
15
 */
16
class DelegatingProvider implements Provider, SuggestableProvider, ListableProvider
17
{
18
    /**
19
     * @var Provider[]
20
     */
21
    protected $providers;
22
23
    /**
24
     * Initialize the provider
25
     */
26 4
    public function __construct()
27
    {
28 4
        $this->providers = [];
29 4
    }
30
31
    /**
32
     * Add a provider with the specified priority. Higher priority means exactly that ;)
33
     *
34
     * @param Provider $provider
35
     * @param int $priority
36
     * @return void
37
     */
38 3
    public function addProvider(Provider $provider, $priority = 0)
39
    {
40 3
        $this->providers[] = [
41 3
            'priority' => $priority,
42 3
            'provider' => $provider
43
        ];
44 3
    }
45
46
    /**
47
     * {@inheritDoc}
48
     */
49 1
    public function supports($object)
50
    {
51 1
        foreach ($this->getProviders() as $provider) {
52 1
            if ($provider->supports($object)) {
53 1
                return true;
54
            }
55
        }
56 1
        return false;
57
    }
58
59
    /**
60
     * {@inheritDoc}
61
     */
62 3
    public function url($object, array $options = array())
63
    {
64 3
        foreach ($this->getProviders() as $provider) {
65 2
            if ($provider->supports($object)) {
66 2
                return $provider->url($object, $options);
67
            }
68
        }
69
70
71 1
        $objectType = is_object($object)
72
            ? get_class($object)
73 1
            : (gettype($object) . ' (' . var_export($object, true) . ')');
74
75 1
        throw new UnsupportedException("Can not render url for {$objectType}");
76
    }
77
78
    /**
79
     * {@inheritDoc}
80
     */
81 1
    public function suggest($pattern)
82
    {
83 1
        $ret = array();
84 1
        foreach ($this->getProviders() as $provider) {
85 1
            if ($provider instanceof SuggestableProvider) {
86 1
                $ret = array_merge($ret, $provider->suggest($pattern));
87
            }
88
        }
89 1
        return $ret;
90
    }
91
92
    /**
93
     * {@inheritDoc}
94
     */
95
    public function all(AuthorizationCheckerInterface $securityContext)
96
    {
97
        $ret = array();
98
        foreach ($this->getProviders() as $provider) {
99
            if ($provider instanceof ListableProvider) {
100
                $ret = array_merge($ret, $provider->all($securityContext));
101
            }
102
        }
103
        return $ret;
104
    }
105
106
    /**
107
     * @return Provider[]|array
108
     */
109 4
    private function getProviders()
110
    {
111 4
        return iterable($this->providers)->sorted('priority')->map('provider')->values();
112
    }
113
}
114