DomainLocator::findWhoisServer()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 2
b 0
f 0
nc 3
nop 1
dl 0
loc 14
ccs 6
cts 6
cp 1
crap 3
rs 10
1
<?php
2
3
namespace MallardDuck\Whois\WhoisServerList;
4
5
use MallardDuck\Whois\Exceptions\MissingArgException;
6
use MallardDuck\Whois\Exceptions\UnknownWhoisException;
7
8
/**
9
 * Whois Server List Locator Class
10
 *
11
 * This class loads a TLD whois list and allows for easy look up.
12
 *
13
 * @author mallardduck <[email protected]>
14
 *
15
 * @copyright lucidinternets.com 2018
16
 *
17
 * @version 1.0.0
18
 */
19
class DomainLocator extends AbstractLocator
20
{
21
    /**
22
     * The path where the tld json file exists.
23
     *
24
     * @var string
25
     */
26
    protected string $whoisListPath;
27
28
    public function __construct()
29
    {
30
        $this->whoisListPath = dirname(__DIR__, 2) . '/blobs/tld.json';
31
        parent::__construct();
32
    }
33
34
    /**
35
     * Finds and returns the last match looked up.
36
     *
37 72
     * @param string $domain Either an ID or a username.
38
     *
39 72
     * @return self Returns the same instance for fluent usage.
40 8
     * @throws UnknownWhoisException|MissingArgException
41
     */
42
    public function findWhoisServer(string $domain): self
43
    {
44 68
        if ('' === $domain) {
45 68
            throw new MissingArgException("Input domain must be provided and cannot be an empty string.");
46 68
        }
47 8
        $tldInfo = $this->whoisCollection->filter(static function ($item, $key) use ($domain) {
48
            return preg_match('/' . $key . '/', $domain);
49 60
        });
50
        if (empty($tldInfo->all())) {
51 60
            throw new UnknownWhoisException("This domain doesn't have a valid TLD whois server.");
52
        }
53
        $this->lastMatch = $tldInfo->first();
54
55
        return $this;
56
    }
57
58
    /**
59
     * Get the Whois server of the domain provided, or previously found domain.
60
     *
61
     * @param ?string $domain The domain being looked up via whois.
62
     *
63 66
     * @return string         Returns the domain name of the whois server.
64
     * @throws MissingArgException
65 66
     * @throws UnknownWhoisException
66 2
     */
67
    public function getWhoisServer(?string $domain = ''): string
68 66
    {
69 50
        if ('' === $domain && empty($this->lastMatch)) {
70
            throw new MissingArgException("Input not parsable to determine whois server.");
71 58
        }
72 4
        if ('' !== $domain) {
73
            $this->findWhoisServer($domain);
0 ignored issues
show
Bug introduced by
It seems like $domain can also be of type null; however, parameter $domain of MallardDuck\Whois\WhoisS...ator::findWhoisServer() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

73
            $this->findWhoisServer(/** @scrutinizer ignore-type */ $domain);
Loading history...
74
        }
75 54
        if ('UNKNOWN' === strtoupper($this->lastMatch)) {
76
            throw new UnknownWhoisException("Unable to determine valid whois server for this domain.");
77
        }
78
79
        return $this->lastMatch;
80
    }
81
}
82