Completed
Push — master ( 4799e1...634839 )
by Kacper
02:53
created

DnsResolver::getIterator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 6
ccs 3
cts 3
cp 1
crap 1
rs 9.4285
c 0
b 0
f 0
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 1
    public function __construct(array $pool)
26
    {
27 1
        $this->_pool = $pool;
28 1
    }
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 1
    public function getIterator()
39
    {
40 1
        $this->resolve();
41
42 1
        return new \ArrayIterator($this->_results);
43
    }
44
45 1
    public function resolve(bool $force = false) : bool
46
    {
47 1
        if (!$force && is_array($this->_results)) {
48
            return true;
49
        }
50
51 1
        $this->_results = [];
52 1
        foreach ($this->_pool as $address => $type) {
53 1
            if (!($result = dns_get_record($address, $type))) {
54
                continue;
55
            }
56
57 1
            $this->_results = array_merge($this->_results, array_map(function ($record) {
58 1
                return [$record['target'], $record['port']];
59 1
            }, $result));
60
        }
61
62 1
        return true;
63
    }
64
}
65