Completed
Push — master ( 484f58...46ef65 )
by Sam
03:14
created

StackableResolver::allowsRecursion()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.072

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 3
nop 0
dl 0
loc 9
ccs 4
cts 5
cp 0.8
crap 3.072
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of PHP DNS Server.
5
 *
6
 * (c) Yif Swery <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace yswery\DNS\Resolver;
13
14
use yswery\DNS\ResourceRecord;
15
16
class StackableResolver implements ResolverInterface
17
{
18
    /**
19
     * @var ResolverInterface[]
20
     */
21
    protected $resolvers;
22
23 15
    public function __construct(array $resolvers = [])
24
    {
25 15
        $this->resolvers = $resolvers;
26 15
    }
27
28
    /**
29
     * @param ResourceRecord[] $question
30
     *
31
     * @return array
32
     */
33 11
    public function getAnswer(array $question): array
34
    {
35 11
        foreach ($this->resolvers as $resolver) {
36 11
            $answer = $resolver->getAnswer($question);
37 11
            if (!empty($answer)) {
38 9
                return $answer;
39
            }
40
        }
41
42 2
        return [];
43
    }
44
45
    /**
46
     * Check if any of the resolvers supports recursion.
47
     *
48
     * @return bool true if any resolver supports recursion
49
     */
50 7
    public function allowsRecursion(): bool
51
    {
52 7
        foreach ($this->resolvers as $resolver) {
53 7
            if ($resolver->allowsRecursion()) {
54
                return true;
55
            }
56
        }
57
58 7
        return false;
59
    }
60
61
    /*
62
     * Check if any resolver knows about a domain
63
     *
64
     * @param  string  $domain the domain to check for
65
     * @return boolean         true if some resolver holds info about $domain
66
     */
67 6
    public function isAuthority($domain): bool
68
    {
69 6
        foreach ($this->resolvers as $resolver) {
70 6
            if ($resolver->isAuthority($domain)) {
71 6
                return true;
72
            }
73
        }
74
75
        return false;
76
    }
77
}
78