Completed
Push — dev-2.0.0 ( e891eb...ad9c49 )
by Daniel
01:47
created

Client   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 80
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0
wmc 13

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A parseWhoisInput() 0 12 2
A lookup() 0 6 1
B validateLookupArgs() 0 16 9
1
<?php
2
namespace MallardDuck\Whois;
3
4
use TrueBV\Punycode;
5
use MallardDuck\Whois\Exceptions\MissingArgException;
6
7
/**
8
 * The Whois Client Class.
9
 *
10
 * @author mallardduck <[email protected]>
11
 *
12
 * @copyright lucidinternets.com 2018
13
 *
14
 * @version 0.4.0
15
 */
16
class Client extends AbstractWhoisClient
17
{
18
    /**
19
     * The Unicode for IDNA.
20
     * @var \TrueBV\Punycode
21
     */
22
    protected $punycode;
23
24
    /**
25
     * The parsed input after validating and encoding.
26
     * @var string
27
     */
28
    public $parsedInput;
29
30
    /**
31
     * Construct the Whois Client Class.
32
     */
33 52
    public function __construct()
34
    {
35 52
        $this->punycode = new Punycode();
36 52
    }
37
    /**
38
     * Performs a Whois look up on the domain provided.
39
     * @param  string $input        The domain or ip being looked up via whois.
40
     * @param  string $whoisServer  The whois server to preform the lookup on.
41
     *
42
     * @return string         The output of the Whois look up.
43
     */
44 28
    public function lookup(string $input = "", string $whoisServer = "") : string
45
    {
46 28
        $this->validateLookupArgs($input, $whoisServer);
47 22
        $this->parseWhoisInput($input);
48
49 22
        return $this->makeRequest($this->parsedInput, $whoisServer);
50
    }
51
52
    /**
53
     * Validates the input for the `lookup` method.
54
     * @param  string $input        A string that should represent a domain name or IP.
55
     * @param  string $whoisServer  A string that represents a domain or IP of a whois server.
56
     */
57 34
    private function validateLookupArgs(string $input = "", string $whoisServer = "") : void
58
    {
59 34
        if (empty($input) || empty($whoisServer)) {
60 12
            $primaryMissing = $input ? false : true;
61 12
            $serverMissing = $whoisServer ? false : true;
62 12
            if ($primaryMissing && $serverMissing) {
63 4
                throw new MissingArgException(
64 4
                    "No input provided. Must provide both primary input (domain or IP) and whois server to this method."
65
                );
66 8
            } elseif ($primaryMissing) {
67 4
                throw new MissingArgException(
68 4
                    "No primary input provided. Cannot lookup empty domain, or IP string."
69
                );
70 4
            } elseif ($serverMissing) {
71 4
                throw new MissingArgException(
72 4
                    "No whois server provided. Must provide IP or domain for whois server with this method."
73
                );
74
            }
75
        }
76 22
    }
77
78
    /**
79
     * Takes the user provided input and parses then encodes the string.
80
     * @param  string $input The user provided input.
81
     *
82
     * @return string Returns the parsed whois input.
83
     */
84 36
    public function parseWhoisInput(string $input) : string
85
    {
86
        // Check domain encoding
87 36
        $encoding = mb_detect_encoding($input);
88
89
        // Punycode the domain if it's Unicode
90 36
        if ("UTF-8" === $encoding) {
91 10
            $input = $this->punycode->encode($input);
92
        }
93 36
        $this->parsedInput = $input;
94
95 36
        return $input;
96
    }
97
}
98