Completed
Push — master ( 7a14ea...626a8c )
by Daniel
02:28
created

DomainLocator::findWhoisServer()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 1
dl 0
loc 15
ccs 9
cts 9
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace MallardDuck\Whois\WhoisServerList;
3
4
use MallardDuck\Whois\Exceptions\MissingArgException;
5
use MallardDuck\Whois\Exceptions\UnknownWhoisException;
6
7
/**
8
 * Whois Server List Locator Class
9
 *
10
 * This class loads a TLD whois list and allows for easy look up.
11
 *
12
 * @author mallardduck <[email protected]>
13
 *
14
 * @copyright lucidinternets.com 2018
15
 *
16
 * @version 1.0.0
17
 */
18
class DomainLocator extends AbstractLocator
19
{
20
21
    /**
22
     * The path where the tld json file exists.
23
     *
24
     * @var string
25
     */
26
    protected $whoisListPath =  __DIR__ . '/../../blobs/tld.json';
27
28
    /**
29
     * Finds and returns the last match looked up.
30
     *
31
     * @param string $domain Either an ID or a username.
32
     *
33
     * @return self Returns the same instance for fluent usage.
34
     */
35 111
    public function findWhoisServer($domain)
36
    {
37 111
        if (empty($domain)) {
38 18
            throw new MissingArgException("Must provide domain argument.");
39
        }
40
41
        $tldInfo = $this->whoisCollection->filter(function ($item, $key) use ($domain) {
42 102
            return preg_match('/'.$key.'/', $domain);
43 102
        });
44 102
        if (empty($tldInfo->all())) {
45 12
            throw new UnknownWhoisException("This domain doesn't have a valid TLD whois server.");
46
        }
47 90
        $this->lastMatch = $tldInfo->all();
48
49 90
        return $this;
50
    }
51
52
    /**
53
     * Get the Whois server of the domain provided, or previously found domain.
54
     *
55
     * @param  string $domain The domain being looked up via whois.
56
     *
57
     * @return string         Returns the domain name of the whois server.
58
     */
59 102
    public function getWhoisServer($domain = '')
60
    {
61 102
        if (!empty($domain) || empty($this->lastMatch)) {
62 78
            $this->findWhoisServer($domain);
63
        }
64 87
        $server = current($this->lastMatch);
65 87
        if ('UNKNOWN' === strtoupper($server)) {
66 6
            throw new UnknownWhoisException("This domain doesn't have a valid whois server.");
67
        }
68
69 81
        return $server;
70
    }
71
}
72