Completed
Push — master ( 528f7d...f4b721 )
by Kacper
04:19
created

DnsResolver::resolve()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 4
nop 1
dl 0
loc 18
rs 8.8571
c 0
b 0
f 0
ccs 0
cts 10
cp 0
crap 30
1
<?php
2
/**
3
 * XMPP Library
4
 *
5
 * Copyright (C) 2016, Some right reserved.
6
 *
7
 * @author Kacper "Kadet" Donat <[email protected]>
8
 *
9
 * Contact with author:
10
 * Xmpp: [email protected]
11
 * E-mail: [email protected]
12
 *
13
 * From Kadet with love.
14
 */
15
16
namespace Kadet\Xmpp\Utils;
17
18
use Traversable;
19
20
class DnsResolver implements \IteratorAggregate
21
{
22
    private $_results = null;
23
    private $_pool;
24
25
    public function __construct(array $pool)
26
    {
27
        $this->_pool = $pool;
28
    }
29
30
    /**
31
     * Retrieve an external iterator
32
     *
33
     * @link  http://php.net/manual/en/iteratoraggregate.getiterator.php
34
     * @return Traversable An instance of an object implementing <b>Iterator</b> or
35
     *        <b>Traversable</b>
36
     * @since 5.0.0
37
     */
38
    public function getIterator()
39
    {
40
        $this->resolve();
41
42
        return new \ArrayIterator($this->_results);
43
    }
44
45
    public function resolve(bool $force = false) : bool
46
    {
47
        if (!$force && is_array($this->_results)) {
48
            return true;
49
        }
50
51
        $this->_results = [];
52
        foreach ($this->_pool as $address => $type) {
53
            if (!($result = dns_get_record($address, $type))) {
54
                continue;
55
            }
56
57
            $this->_results = array_merge($this->_results, array_map(function ($record) {
58
                return [$record['target'], $record['port']];
59
            }, $result));
60
        }
61
        return true;
62
    }
63
}
64