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

ChainSiteAccessProvider::isDefined()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 1
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
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
}