Completed
Push — EZP-31044-site-access-provider ( 8220ce )
by
unknown
14:19
created

ChainSiteAccessProvider   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 45
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getSiteAccesses() 0 9 2
A isDefined() 0 10 3
A getSiteAccess() 0 10 3
1
<?php
2
3
/**
4
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
5
 * @license For full copyright and license information view LICENSE file distributed with this source code.
6
 */
7
declare(strict_types=1);
8
9
namespace eZ\Publish\Core\MVC\Symfony\SiteAccess\Provider;
10
11
use AppendIterator;
12
use eZ\Publish\Core\MVC\Symfony\SiteAccess;
13
use eZ\Publish\Core\MVC\Symfony\SiteAccess\SiteAccessProviderInterface;
14
use Iterator;
15
use RuntimeException;
16
17
final class ChainSiteAccessProvider implements SiteAccessProviderInterface
18
{
19
    /** @var \eZ\Publish\Core\MVC\Symfony\SiteAccess\SiteAccessProviderInterface[] */
20
    private $providers;
21
22
    /**
23
     * @param \eZ\Publish\Core\MVC\Symfony\SiteAccess\SiteAccessProviderInterface[] $providers
24
     */
25
    public function __construct(iterable $providers)
26
    {
27
        $this->providers = $providers;
28
    }
29
30
    public function getSiteAccesses(): Iterator
31
    {
32
        $iterator = new AppendIterator();
33
        foreach($this->providers as $provider) {
34
            $iterator->append($provider->getSiteAccesses());
35
        }
36
37
        return $iterator;
38
    }
39
40
    public function isDefined(string $name): bool
41
    {
42
        foreach ($this->providers as $provider) {
43
            if ($provider->isDefined($name)) {
44
                return true;
45
            }
46
        }
47
48
        return false;
49
    }
50
51
    public function getSiteAccess(string $name): SiteAccess
52
    {
53
        foreach ($this->providers as $provider) {
54
            if ($provider->isDefined($name)) {
55
                return $provider->getSiteAccess($name);
56
            }
57
        }
58
59
        throw new RuntimeException("Undefined siteaccess: $name");
60
    }
61
}