Passed
Push — master ( be2bff...aebff6 )
by Daniel
03:03
created

Locator::getLoadStatus()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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