Completed
Push — master ( 609783...50563c )
by
unknown
14:28
created

ChainSiteAccessProvider   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 49
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A isDefined() 0 10 3
A __construct() 0 4 1
A getSiteAccesses() 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 eZ\Publish\Core\Base\Exceptions\NotFoundException;
12
use eZ\Publish\Core\MVC\Symfony\SiteAccess;
13
use eZ\Publish\Core\MVC\Symfony\SiteAccess\SiteAccessProviderInterface;
14
use Traversable;
15
16
final class ChainSiteAccessProvider implements SiteAccessProviderInterface
17
{
18
    /** @var \eZ\Publish\Core\MVC\Symfony\SiteAccess\SiteAccessProviderInterface[] */
19
    private $providers;
20
21
    /**
22
     * @param \eZ\Publish\Core\MVC\Symfony\SiteAccess\SiteAccessProviderInterface[] $providers
23
     */
24
    public function __construct(iterable $providers = [])
25
    {
26
        $this->providers = $providers;
27
    }
28
29
    public function getSiteAccesses(): Traversable
30
    {
31
        foreach ($this->providers as $provider) {
32
            foreach ($provider->getSiteAccesses() as $siteAccess) {
33
                yield $siteAccess;
34
            }
35
        }
36
37
        yield from [];
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
    /**
52
     * @throws \eZ\Publish\Core\Base\Exceptions\NotFoundException
53
     */
54
    public function getSiteAccess(string $name): SiteAccess
55
    {
56
        foreach ($this->providers as $provider) {
57
            if ($provider->isDefined($name)) {
58
                return $provider->getSiteAccess($name);
59
            }
60
        }
61
62
        throw new NotFoundException('Site Access', $name);
63
    }
64
}
65