DelegatingProvider   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Test Coverage

Coverage 77.42%

Importance

Changes 4
Bugs 1 Features 0
Metric Value
eloc 25
c 4
b 1
f 0
dl 0
loc 84
ccs 24
cts 31
cp 0.7742
rs 10
wmc 15

6 Methods

Rating   Name   Duplication   Size   Complexity  
A all() 0 9 3
A addProvider() 0 3 1
A url() 0 13 4
A suggest() 0 9 3
A __construct() 0 3 1
A supports() 0 8 3
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\Routing\Generator\UrlGeneratorInterface;
10
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
11
use Zicht\Bundle\UrlBundle\Exception\UnsupportedException;
12
use Zicht\Bundle\FrameworkExtraBundle\Util\SortedList;
13
14
/**
15
 * A provider that delegates to a number of registered providers, ordered by priority.
16
 */
17
class DelegatingProvider implements Provider, SuggestableProvider, ListableProvider
18
{
19
    /**
20
     * @var Provider[]
21
     */
22
    protected $providers = array();
23
24
    /**
25
     * Initialize the provider
26 4
     */
27
    public function __construct()
28 4
    {
29 4
        $this->providers = new SortedList();
0 ignored issues
show
Deprecated Code introduced by
The class Zicht\Bundle\FrameworkExtraBundle\Util\SortedList has been deprecated: Use sorting functionality from `zicht/itertools` instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

29
        $this->providers = /** @scrutinizer ignore-deprecated */ new SortedList();
Loading history...
Documentation Bug introduced by
It seems like new Zicht\Bundle\Framewo...undle\Util\SortedList() of type Zicht\Bundle\FrameworkExtraBundle\Util\SortedList is incompatible with the declared type Zicht\Bundle\UrlBundle\Url\Provider[] of property $providers.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
30
    }
31
32
    /**
33
     * Add a provider with the specified priority. Higher priority means exactly that ;)
34
     *
35
     * @param Provider $provider
36
     * @param int $priority
37
     * @return void
38 3
     */
39
    public function addProvider(Provider $provider, $priority = 0)
40 3
    {
41 3
        $this->providers->insert($provider, $priority);
42
    }
43
44
    /**
45
     * {@inheritDoc}
46 1
     */
47
    public function supports($object)
48 1
    {
49 1
        foreach ($this->providers as $provider) {
50 1
            if ($provider->supports($object)) {
51
                return true;
52
            }
53 1
        }
54
        return false;
55
    }
56
57
    /**
58
     * {@inheritDoc}
59 3
     */
60
    public function url($object, array $options = array(), $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH)
61 3
    {
62 2
        foreach ($this->providers as $provider) {
63 2
            if ($provider->supports($object)) {
64
                return $provider->url($object, $options, $referenceType);
65
            }
66
        }
67
68 1
        $objectType = is_object($object)
69
            ? get_class($object)
70 1
            : (gettype($object) . ' (' . var_export($object, true) . ')');
71
72 1
        throw new UnsupportedException("Can not render url for {$objectType}");
73
    }
74
75
    /**
76
     * @{inheritDoc}
77
     */
78 1
    public function suggest($pattern)
79
    {
80 1
        $ret = array();
81 1
        foreach ($this->providers as $provider) {
82 1
            if ($provider instanceof SuggestableProvider) {
83 1
                $ret = array_merge($ret, $provider->suggest($pattern));
84
            }
85
        }
86 1
        return $ret;
87
    }
88
89
    /**
90
     * @{inheritDoc}
91
     */
92
    public function all(AuthorizationCheckerInterface $securityContext)
93
    {
94
        $ret = array();
95
        foreach ($this->providers as $provider) {
96
            if ($provider instanceof ListableProvider) {
97
                $ret = array_merge($ret, $provider->all($securityContext));
98
            }
99
        }
100
        return $ret;
101
    }
102
}
103