Completed
Push — master ( 584eab...218d7a )
by Daniel
02:26
created

Locator::findWhoisServer()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 2
nop 1
dl 0
loc 11
ccs 7
cts 7
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
 * @copyright lucidinternets.com 2018
14
 * @version 1.0.0
15
 */
16
class Locator
17
{
18
19
     /**
20
      * The status of loading the whois server list.
21
      *
22
      * @var bool
23
      */
24
    private $loadStatus = false;
25
26
    /**
27
     * The path where the tld json file exists.
28
     *
29
     * @var string
30
     */
31
    private $tldListPath =  __DIR__ . '/../../blobs/tld.json';
32
33
    /**
34
     * A collection of the TLDs and whois server list.
35
     *
36
     * @var \Tightenco\Collect\Support\Collection
37
     */
38
    private $tldCollection;
39
40
    /**
41
     * The results of the last looked up domain.
42
     *
43
     * @var array
44
     */
45
    private $lastMatch;
46
47
    /**
48
     * Build the TLD Whois Server Locator class.
49 42
     */
50
    public function __construct()
51 42
    {
52 42
        $file_data = file_get_contents($this->tldListPath);
53 42
        $tldData = json_decode($file_data);
54 42
        if ($tldData !== null && json_last_error() === JSON_ERROR_NONE) {
55 14
            $this->loadStatus = true;
56 42
        }
57 42
        $this->tldCollection = collect((array) $tldData);
58
    }
59
60
    /**
61
     * Returns the TLD list load status.
62
     *
63
     * @return bool The class status of loading the list and decoding the json.
64 3
     */
65
    public function getLoadStatus()
66 3
    {
67
        return $this->loadStatus;
68
    }
69
70
    /**
71
     * Gets and returns the last match looked up.
72
     *
73
     * @return array The results of the last looked up domain.
74 3
     */
75
    public function getLastMatch()
76 3
    {
77
        return $this->lastMatch;
78
    }
79
80
    /**
81
     * Finds and returns the last match looked up.
82
     *
83
     * @param string $domain Either an ID or a username
84
     * @return self Returns the same instance for fluent usage.
85 30
     */
86
    public function findWhoisServer($domain)
87 30
    {
88 15
        if (empty($domain) || is_null($domain)) {
89
            throw new MissingArgException("Must provide domain argument.");
90
        }
91 24
92 24
        $tldInfo = $this->tldCollection->filter(function ($item, $key) use ($domain) {
93 24
            return preg_match('/'.$key.'/', $domain);
94 24
        });
95 24
        $this->lastMatch = $tldInfo->all();
96
        return $this;
97
    }
98
99
    /**
100
     * Get the Whois server of the domain provided, or previously found domain.
101
     *
102
     * @param  string $domain The domain being looked up via whois.
103
     * @return string         Returns the domain name of the whois server.
104 21
     */
105
    public function getWhoisServer($domain = '')
106 21
    {
107 12
        if (!empty($domain) || empty($this->lastMatch)) {
108 4
            $this->findWhoisServer($domain);
109
        }
110 21
        $server = current($this->lastMatch);
111
        if ('UNKNOWN' == strtoupper($server) || empty($server)) {
112
            throw new UnknownWhoisException("This domain doesn't have a valid TLD whois server.");
113
        }
114
        return $server;
115
    }
116
}
117