Completed
Push — dynamic_siteaccesses ( 16c7ff )
by
unknown
20:23
created

ChainSiteAccessProvider   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 41
Duplicated Lines 24.39 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 10
loc 41
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 8 2
A isDefined() 0 10 3
A getSiteAccess() 10 10 3

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 eZ\Publish\Core\MVC\Symfony\SiteAccessList;
15
16
final class ChainSiteAccessProvider implements SiteAccessProviderInterface
17
{
18
    /** @var \eZ\Publish\Core\MVC\Symfony\SiteAccess\SiteAccessProviderInterface[] */
19
    private $providers;
20
21
    public function __construct(iterable $providers)
22
    {
23
        $this->providers = $providers;
24
    }
25
26
    public function getSiteAccesses(): \Iterator
27
    {
28
        $iterator = new AppendIterator();
29
        foreach($this->providers as $provider) {
30
            $iterator->append($provider->getSiteAccesses());
31
        }
32
        return $iterator;
33
    }
34
35
    public function isDefined(string $name): bool
36
    {
37
        foreach ($this->providers as $provider) {
38
            if ($provider->isDefined($name)) {
39
                return true;
40
            }
41
        }
42
43
        return false;
44
    }
45
46 View Code Duplication
    public function getSiteAccess(string $name): SiteAccess
47
    {
48
        foreach ($this->providers as $provider) {
49
            if ($provider->isDefined($name)) {
50
                return $provider->getSiteAccess($name);
51
            }
52
        }
53
54
        throw new \RuntimeException("Undefined siteaccess: $name");
55
    }
56
}
57