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

DomainLocator   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 9
cts 9
cp 1
rs 10
c 0
b 0
f 0
wmc 3

1 Method

Rating   Name   Duplication   Size   Complexity  
A findWhoisServer() 0 15 3
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