Completed
Push — master ( 282f78...6dce89 )
by Sam
05:20
created

StackableResolver::allowsRecursion()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

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