Passed
Branch master (6982d9)
by Ivan
02:35
created

VoterContainer   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
dl 0
loc 35
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A isMatch() 0 9 3
A matches() 0 9 3
A addVoter() 0 3 1
A __construct() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Everlution\Navigation\Match;
6
7
use Everlution\Navigation\Item\MatchableInterface;
8
9
/**
10
 * Class VoterContainer.
11
 *
12
 * @author Ivan Barlog <[email protected]>
13
 */
14
class VoterContainer implements UrlMatchVoterInterface
15
{
16
    /** @var VoterInterface[] */
17
    private $voters = [];
18
19
    public function __construct(array $voters = [])
20
    {
21
        array_map([$this, 'addVoter'], $voters);
22
    }
23
24
    public function addVoter(VoterInterface $voter): void
25
    {
26
        $this->voters[] = $voter;
27
    }
28
29
    public function matches(string $url, MatchableInterface $item): bool
30
    {
31
        foreach ($item->getMatches() as $match) {
32
            if ($this->isMatch($url, $match)) {
33
                return true;
34
            }
35
        }
36
37
        return false;
38
    }
39
40
    private function isMatch(string $url, MatchInterface $match): bool
41
    {
42
        foreach ($this->voters as $voter) {
43
            if ($voter->matches($url, $match)) {
44
                return true;
45
            }
46
        }
47
48
        return false;
49
    }
50
}
51