UrlProviderContainer::getUrl()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 3
nc 3
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Everlution\Navigation\Url;
6
7
use Everlution\Navigation\Item\ItemInterface;
8
9
/**
10
 * Class UrlProviderContainer.
11
 *
12
 * @author Ivan Barlog <[email protected]>
13
 */
14
class UrlProviderContainer
15
{
16
    /** @var UrlProviderInterface[] */
17
    private $providers = [];
18
19
    public function __construct(array $providers = [])
20
    {
21
        array_map([$this, 'addProvider'], $providers);
22
    }
23
24
    public function addProvider(UrlProviderInterface $provider): void
25
    {
26
        $this->providers[] = $provider;
27
    }
28
29
    /**
30
     * @param ItemInterface $item
31
     *
32
     * @return string
33
     *
34
     * @throws CannotProvideUrlForItemException
35
     */
36
    public function getUrl(ItemInterface $item): string
37
    {
38
        foreach ($this->providers as $provider) {
39
            if ($provider->supports($item)) {
40
                return $provider->getUrl($item);
41
            }
42
        }
43
44
        throw new CannotProvideUrlForItemException($item);
45
    }
46
}
47