Completed
Push — master ( 75085c...14b9d3 )
by Daniel
05:18
created

AbstractLocator::getWhoisServer()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 6
nc 4
nop 1
dl 0
loc 11
ccs 7
cts 7
cp 1
crap 4
rs 9.2
c 0
b 0
f 0
1
<?php
2
namespace MallardDuck\Whois\WhoisServerList;
3
4
use MallardDuck\Whois\Exceptions\UnknownWhoisException;
5
6
/**
7
 * Whois Server List Locator Class
8
 *
9
 * This class loads a TLD whois list and allows for easy look up.
10
 *
11
 * @author mallardduck <[email protected]>
12
 *
13
 * @copyright lucidinternets.com 2018
14
 *
15
 * @version 1.0.0
16
 */
17
abstract class AbstractLocator
18
{
19
20
     /**
21
      * The status of loading the whois server list.
22
      *
23
      * @var bool
24
      */
25
    private $loadStatus = false;
26
27
    /**
28
     * The path where the tld json file exists.
29
     *
30
     * @var string
31
     */
32
    protected $whoisListPath;
33
34
    /**
35
     * A collection of the TLDs and whois server list.
36
     *
37
     * @var \Tightenco\Collect\Support\Collection
38
     */
39
    protected $whoisCollection;
40
41
    /**
42
     * The results of the last looked up domain.
43
     *
44
     * @var array
45
     */
46
    protected $lastMatch;
47
48
    /**
49
     * Build the TLD Whois Server Locator class.
50
     */
51 165
    public function __construct()
52
    {
53 165
        $fileData = file_get_contents($this->whoisListPath);
54 165
        $tldData = json_decode($fileData);
55 165
        if (null !== $tldData && json_last_error() === JSON_ERROR_NONE) {
56 165
            $this->loadStatus = true;
57
        }
58 165
        $this->whoisCollection = collect((array) $tldData);
59 165
    }
60
61
    /**
62
     * Returns the TLD list load status.
63
     *
64
     * @return bool The class status of loading the list and decoding the json.
65
     */
66 3
    public function getLoadStatus()
67
    {
68 3
        return $this->loadStatus;
69
    }
70
71
    /**
72
     * Gets and returns the last match looked up.
73
     *
74
     * @return array The results of the last looked up domain.
75
     */
76 3
    public function getLastMatch()
77
    {
78 3
        return $this->lastMatch;
79
    }
80
81
    /**
82
     * Finds and returns the last match looked up.
83
     *
84
     * @param string $domain Either an ID or a username.
85
     *
86
     * @return self Returns the same instance for fluent usage.
87
     */
88
    abstract public function findWhoisServer($domain);
89
90
    /**
91
     * Get the Whois server of the domain provided, or previously found domain.
92
     *
93
     * @param  string $domain The domain being looked up via whois.
94
     *
95
     * @return string         Returns the domain name of the whois server.
96
     */
97 102
    public function getWhoisServer($domain = '')
98
    {
99 102
        if (!empty($domain) || empty($this->lastMatch)) {
100 78
            $this->findWhoisServer($domain);
101
        }
102 87
        $server = current($this->lastMatch);
103 87
        if ('UNKNOWN' === strtoupper($server)) {
104 6
            throw new UnknownWhoisException("This domain doesn't have a valid whois server.");
105
        }
106
107 81
        return $server;
108
    }
109
}
110