From a7615d031e8bfecda5cb7d4b063da9aff8d21a74 Mon Sep 17 00:00:00 2001 From: Dan Pock Date: Thu, 3 May 2018 20:50:01 -0400 Subject: [PATCH 1/5] Add new structure for the Whois Server Locators Using abstract classes will make implementing IPv4, IPv6, and TLD lookup easier. Once all three are properly implemented, then I can build a location service that the client will use. New Lookup from client process: $domainInput -> LookUpDispatchService() -> (Picks correct look up by identifying domains, ipv4 and ipv6) -> getWhoisServer() -> $whoisServer Ta-Da! --- CHANGELOG.md | 5 ++ src/WhoisServerList/AbstractLocator.php | 109 ++++++++++++++++++++++++ src/WhoisServerList/DomainLocator.php | 51 +++++++++++ 3 files changed, 165 insertions(+) create mode 100644 src/WhoisServerList/AbstractLocator.php create mode 100644 src/WhoisServerList/DomainLocator.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 855e398..355ddaa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,11 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Changed +- Improved the flexibility of the WhoisServerList tests. + +### Added +- Create an AbstractLocator and DomainLocator for more flexibility! ## [0.1.5] - 2018-05-01 ### Removed diff --git a/src/WhoisServerList/AbstractLocator.php b/src/WhoisServerList/AbstractLocator.php new file mode 100644 index 0000000..1bbb726 --- /dev/null +++ b/src/WhoisServerList/AbstractLocator.php @@ -0,0 +1,109 @@ + + * + * @copyright lucidinternets.com 2018 + * + * @version 1.0.0 + */ +abstract class AbstractLocator +{ + + /** + * The status of loading the whois server list. + * + * @var bool + */ + private $loadStatus = false; + + /** + * The path where the tld json file exists. + * + * @var string + */ + protected $whoisListPath; + + /** + * A collection of the TLDs and whois server list. + * + * @var \Tightenco\Collect\Support\Collection + */ + protected $whoisCollection; + + /** + * The results of the last looked up domain. + * + * @var array + */ + protected $lastMatch; + + /** + * Build the TLD Whois Server Locator class. + */ + public function __construct() + { + $fileData = file_get_contents($this->whoisListPath); + $tldData = json_decode($fileData); + if (null !== $tldData && json_last_error() === JSON_ERROR_NONE) { + $this->loadStatus = true; + } + $this->whoisCollection = collect((array) $tldData); + } + + /** + * Returns the TLD list load status. + * + * @return bool The class status of loading the list and decoding the json. + */ + public function getLoadStatus() + { + return $this->loadStatus; + } + + /** + * Gets and returns the last match looked up. + * + * @return array The results of the last looked up domain. + */ + public function getLastMatch() + { + return $this->lastMatch; + } + + /** + * Finds and returns the last match looked up. + * + * @param string $domain Either an ID or a username. + * + * @return self Returns the same instance for fluent usage. + */ + abstract public function findWhoisServer($domain); + + /** + * Get the Whois server of the domain provided, or previously found domain. + * + * @param string $domain The domain being looked up via whois. + * + * @return string Returns the domain name of the whois server. + */ + public function getWhoisServer($domain = '') + { + if (!empty($domain) || empty($this->lastMatch)) { + $this->findWhoisServer($domain); + } + $server = current($this->lastMatch); + if ('UNKNOWN' === strtoupper($server)) { + throw new UnknownWhoisException("This domain doesn't have a valid whois server."); + } + + return $server; + } +} diff --git a/src/WhoisServerList/DomainLocator.php b/src/WhoisServerList/DomainLocator.php new file mode 100644 index 0000000..8b5600a --- /dev/null +++ b/src/WhoisServerList/DomainLocator.php @@ -0,0 +1,51 @@ + + * + * @copyright lucidinternets.com 2018 + * + * @version 1.0.0 + */ +class DomainLocator extends AbstractLocator +{ + + /** + * The path where the tld json file exists. + * + * @var string + */ + protected $whoisListPath = __DIR__ . '/../../blobs/tld.json'; + + /** + * Finds and returns the last match looked up. + * + * @param string $domain Either an ID or a username. + * + * @return self Returns the same instance for fluent usage. + */ + public function findWhoisServer($domain) + { + if (empty($domain)) { + throw new MissingArgException("Must provide domain argument."); + } + + $tldInfo = $this->whoisCollection->filter(function ($item, $key) use ($domain) { + return preg_match('/'.$key.'/', $domain); + }); + if (empty($tldInfo->all())) { + throw new UnknownWhoisException("This domain doesn't have a valid TLD whois server."); + } + $this->lastMatch = $tldInfo->all(); + + return $this; + } +} From ed310a7c4cdc57652bfa3e694c01dcf7248597e8 Mon Sep 17 00:00:00 2001 From: Dan Pock Date: Thu, 3 May 2018 20:55:51 -0400 Subject: [PATCH 2/5] Clean up stray PHP 5.x computability code --- CHANGELOG.md | 3 +++ tests/BaseTest.php | 13 ++++++------- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 355ddaa..71bf8b7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ### Added - Create an AbstractLocator and DomainLocator for more flexibility! +### Removed +- Cleaned up some more PHP 5.6 support code from tests. + ## [0.1.5] - 2018-05-01 ### Removed - Support for PHP 5.6 ending the PHP 5.x support here. diff --git a/tests/BaseTest.php b/tests/BaseTest.php index b91ea98..84df53e 100644 --- a/tests/BaseTest.php +++ b/tests/BaseTest.php @@ -14,15 +14,14 @@ */ abstract class BaseTest extends TestCase { + + /** + * [getMethod description] + * @return \League\Uri\Components\Exception [description] + */ public function getUriException() { - $host = new Host(); - $isNewLib = (method_exists($host, 'getRegistrableDomain')) ? true : false; - if ($isNewLib) { - return \League\Uri\Components\Exception::class; - } - - return \Exception::class; + return \League\Uri\Components\Exception::class; } /** From a81e84b93737f453497d51281b238878e2e7b8e4 Mon Sep 17 00:00:00 2001 From: Dan Pock Date: Thu, 3 May 2018 20:58:02 -0400 Subject: [PATCH 3/5] Refactor clients to use AbstractLocator The clients now use the abstract locator based DomainLocator. Eventually I can expand this out to have more than one locator type to compensate the subtle differences in how to mange the IP and Domain based locations differently. --- CHANGELOG.md | 3 +++ src/AbstractClient.php | 9 +++++---- src/Client.php | 2 +- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 71bf8b7..c988723 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ## [Unreleased] ### Changed - Improved the flexibility of the WhoisServerList tests. +- Refactor AbstractClient and client to use the AbstractLocator and DomainLocator. +- Update AbstractClient property name - from tldLocator to whoisLocator - to better reflect usage. + ### Added - Create an AbstractLocator and DomainLocator for more flexibility! diff --git a/src/AbstractClient.php b/src/AbstractClient.php index ffc8a1a..3f3e7b3 100644 --- a/src/AbstractClient.php +++ b/src/AbstractClient.php @@ -4,7 +4,8 @@ use TrueBV\Punycode; use League\Uri\Components\Host; use Hoa\Socket\Client as SocketClient; -use MallardDuck\Whois\WhoisServerList\Locator; +use MallardDuck\Whois\WhoisServerList\AbstractLocator; +use MallardDuck\Whois\WhoisServerList\DomainLocator; use MallardDuck\Whois\Exceptions\MissingArgException; /** @@ -21,9 +22,9 @@ class AbstractClient /** * The TLD Whois locator class. - * @var Locator + * @var AbstractLocator */ - protected $tldLocator; + protected $whoisLocator; /** * The Unicode for IDNA. @@ -55,7 +56,7 @@ class AbstractClient public function __construct() { $this->punycode = new Punycode(); - $this->tldLocator = new Locator(); + $this->whoisLocator = new DomainLocator(); } /** diff --git a/src/Client.php b/src/Client.php index e979850..ca78fd3 100644 --- a/src/Client.php +++ b/src/Client.php @@ -37,7 +37,7 @@ public function lookup($domain = '') $this->parseWhoisDomain($domain); // Get the domains whois server. - $whoisServer = $this->tldLocator->getWhoisServer($this->parsedDomain); + $whoisServer = $this->whoisLocator->getWhoisServer($this->parsedDomain); // Get the full output of the whois lookup. $response = $this->makeWhoisRequest($this->parsedDomain, $whoisServer); From 895b4dc9d3bcceed58997bf7dfe4acedf6a0352c Mon Sep 17 00:00:00 2001 From: Dan Pock Date: Thu, 3 May 2018 21:09:57 -0400 Subject: [PATCH 4/5] Remove the original Locator class --- CHANGELOG.md | 1 + src/WhoisServerList/Locator.php | 125 -------------------------------- 2 files changed, 1 insertion(+), 125 deletions(-) delete mode 100644 src/WhoisServerList/Locator.php diff --git a/CHANGELOG.md b/CHANGELOG.md index c988723..0e2c681 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. ### Removed - Cleaned up some more PHP 5.6 support code from tests. +- Removed old style locator class. ## [0.1.5] - 2018-05-01 ### Removed diff --git a/src/WhoisServerList/Locator.php b/src/WhoisServerList/Locator.php deleted file mode 100644 index be3a093..0000000 --- a/src/WhoisServerList/Locator.php +++ /dev/null @@ -1,125 +0,0 @@ - - * - * @copyright lucidinternets.com 2018 - * - * @version 1.0.0 - */ -class Locator -{ - - /** - * The status of loading the whois server list. - * - * @var bool - */ - private $loadStatus = false; - - /** - * The path where the tld json file exists. - * - * @var string - */ - private $tldListPath = __DIR__ . '/../../blobs/tld.json'; - - /** - * A collection of the TLDs and whois server list. - * - * @var \Tightenco\Collect\Support\Collection - */ - private $tldCollection; - - /** - * The results of the last looked up domain. - * - * @var array - */ - private $lastMatch; - - /** - * Build the TLD Whois Server Locator class. - */ - public function __construct() - { - $fileData = file_get_contents($this->tldListPath); - $tldData = json_decode($fileData); - if (null !== $tldData && json_last_error() === JSON_ERROR_NONE) { - $this->loadStatus = true; - } - $this->tldCollection = collect((array) $tldData); - } - - /** - * Returns the TLD list load status. - * - * @return bool The class status of loading the list and decoding the json. - */ - public function getLoadStatus() - { - return $this->loadStatus; - } - - /** - * Gets and returns the last match looked up. - * - * @return array The results of the last looked up domain. - */ - public function getLastMatch() - { - return $this->lastMatch; - } - - /** - * Finds and returns the last match looked up. - * - * @param string $domain Either an ID or a username. - * - * @return self Returns the same instance for fluent usage. - */ - public function findWhoisServer($domain) - { - if (empty($domain)) { - throw new MissingArgException("Must provide domain argument."); - } - - $tldInfo = $this->tldCollection->filter(function ($item, $key) use ($domain) { - return preg_match('/'.$key.'/', $domain); - }); - if (empty($tldInfo->all())) { - throw new UnknownWhoisException("This domain doesn't have a valid TLD whois server."); - } - $this->lastMatch = $tldInfo->all(); - - return $this; - } - - /** - * Get the Whois server of the domain provided, or previously found domain. - * - * @param string $domain The domain being looked up via whois. - * - * @return string Returns the domain name of the whois server. - */ - public function getWhoisServer($domain = '') - { - if (!empty($domain) || empty($this->lastMatch)) { - $this->findWhoisServer($domain); - } - $server = current($this->lastMatch); - if ('UNKNOWN' === strtoupper($server)) { - throw new UnknownWhoisException("This domain doesn't have a valid TLD whois server."); - } - - return $server; - } -} From 919da695b42279fdccb827272d5047fdccbd8252 Mon Sep 17 00:00:00 2001 From: Dan Pock Date: Thu, 3 May 2018 21:10:51 -0400 Subject: [PATCH 5/5] Update tests to account for removed Locator class --- CHANGELOG.md | 2 +- tests/BaseTest.php | 1 - tests/WhoisLocatorExceptionTest.php | 18 +++++++++--------- tests/WhoisLocatorLookupsTest.php | 6 +++--- tests/WhoisLocatorTest.php | 10 +++++----- 5 files changed, 18 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e2c681..3d64c46 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,7 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Improved the flexibility of the WhoisServerList tests. - Refactor AbstractClient and client to use the AbstractLocator and DomainLocator. - Update AbstractClient property name - from tldLocator to whoisLocator - to better reflect usage. - +- Refactor tests to use DomainLocator instead of removed Locator class. ### Added - Create an AbstractLocator and DomainLocator for more flexibility! diff --git a/tests/BaseTest.php b/tests/BaseTest.php index 84df53e..42807eb 100644 --- a/tests/BaseTest.php +++ b/tests/BaseTest.php @@ -2,7 +2,6 @@ namespace MallardDuck\Whois\Test; use PHPUnit\Framework\TestCase; -use League\Uri\Components\Host; /** * Corresponding Class to test the whois Client class diff --git a/tests/WhoisLocatorExceptionTest.php b/tests/WhoisLocatorExceptionTest.php index d7418a9..55e246a 100644 --- a/tests/WhoisLocatorExceptionTest.php +++ b/tests/WhoisLocatorExceptionTest.php @@ -2,7 +2,7 @@ namespace MallardDuck\Whois\Test; use PHPUnit\Framework\TestCase; -use MallardDuck\Whois\WhoisServerList\Locator; +use MallardDuck\Whois\WhoisServerList\DomainLocator; use MallardDuck\Whois\Exceptions\MissingArgException; use MallardDuck\Whois\Exceptions\UnknownWhoisException; @@ -24,7 +24,7 @@ public function testBlankStringThrowsException() { $this->expectException(MissingArgException::class); - $var = new Locator; + $var = new DomainLocator; $results = $var->findWhoisServer(''); unset($var, $results); } @@ -34,7 +34,7 @@ public function testBlankStringThrowsException() */ public function testFindServerThenGetWhoisServerThenEmpty() { - $var = new Locator; + $var = new DomainLocator; $results = $var->findWhoisServer("com.com")->getWhoisServer(); $this->assertTrue(is_string($results) && !empty($results)); $this->assertTrue("whois.verisign-grs.com" === $results); @@ -56,7 +56,7 @@ public function testNullStringThrowsException() $this->expectException(\Exception::class); } - $var = new Locator; + $var = new DomainLocator; $results = $var->findWhoisServer(null); unset($var, $results); } @@ -66,7 +66,7 @@ public function testNullStringThrowsException() */ public function testFindServerThenGetWhoisServerThenNull() { - $var = new Locator; + $var = new DomainLocator; $results = $var->findWhoisServer("com.com")->getWhoisServer(); $this->assertTrue(is_string($results) && !empty($results)); $this->assertTrue("whois.verisign-grs.com" === $results); @@ -86,7 +86,7 @@ public function testFindServerThenGetWhoisServerThenNull() */ public function testGetWhoisServerDirect() { - $var = new Locator; + $var = new DomainLocator; $results = $var->getWhoisServer("bing.com"); $this->assertTrue(is_string($results) && !empty($results)); $this->assertTrue("whois.verisign-grs.com" === $results); @@ -94,7 +94,7 @@ public function testGetWhoisServerDirect() $this->expectException(MissingArgException::class); - $var = new Locator; + $var = new DomainLocator; $results = $var->getWhoisServer(); unset($var, $results); } @@ -104,7 +104,7 @@ public function testGetWhoisServerDirect() */ public function testGetWhoisServerDirectNoException() { - $var = new Locator; + $var = new DomainLocator; $results = $var->getWhoisServer("bing.com"); $orgResults = $results; $this->assertTrue(is_string($results) && !empty($results)); @@ -121,7 +121,7 @@ public function testGetWhoisServerDirectNoException() */ public function testGetWhoisServerDirectUnicodeException() { - $var = new Locator; + $var = new DomainLocator; $this->expectException(UnknownWhoisException::class); $results = $var->getWhoisServer('xn--e1afmkfd.xn--80akhbyknj4f'); diff --git a/tests/WhoisLocatorLookupsTest.php b/tests/WhoisLocatorLookupsTest.php index 7175e49..fbb00b2 100644 --- a/tests/WhoisLocatorLookupsTest.php +++ b/tests/WhoisLocatorLookupsTest.php @@ -2,7 +2,7 @@ namespace MallardDuck\Whois\Test; use PHPUnit\Framework\TestCase; -use MallardDuck\Whois\WhoisServerList\Locator; +use MallardDuck\Whois\WhoisServerList\DomainLocator; /** * Corresponding Class to test the Locator class @@ -24,7 +24,7 @@ class WhoisLocatorLookupsTest extends TestCase */ public function testFindAndGetCorrectDomainWhoisServer($domain, $server) { - $var = new Locator; + $var = new DomainLocator; $results = $var->findWhoisServer($domain)->getWhoisServer(); $this->assertTrue(is_string($results) && !empty($results)); $this->assertTrue($server === $results); @@ -39,7 +39,7 @@ public function testFindAndGetCorrectDomainWhoisServer($domain, $server) */ public function testGetCorrectDomainWhoisServer($domain, $server) { - $var = new Locator; + $var = new DomainLocator; $results = $var->getWhoisServer($domain); $this->assertTrue(is_string($results) && !empty($results)); $this->assertTrue($server === $results); diff --git a/tests/WhoisLocatorTest.php b/tests/WhoisLocatorTest.php index 0c99d79..80b2e17 100644 --- a/tests/WhoisLocatorTest.php +++ b/tests/WhoisLocatorTest.php @@ -2,7 +2,7 @@ namespace MallardDuck\Whois\Test; use PHPUnit\Framework\TestCase; -use MallardDuck\Whois\WhoisServerList\Locator; +use MallardDuck\Whois\WhoisServerList\DomainLocator; /** * Corresponding Class to test the Locator class @@ -20,7 +20,7 @@ class WhoisLocatorTest extends TestCase */ public function testIsThereAnySyntaxError() { - $var = new Locator; + $var = new DomainLocator; $this->assertTrue(is_object($var)); unset($var); } @@ -30,7 +30,7 @@ public function testIsThereAnySyntaxError() */ public function testLoadedListFile() { - $var = new Locator; + $var = new DomainLocator; $this->assertTrue(is_object($var) && $var->getLoadStatus()); unset($var); } @@ -40,13 +40,13 @@ public function testLoadedListFile() */ public function testFindWhoisServer() { - $var = new Locator; + $var = new DomainLocator; $var->findWhoisServer("google.com"); $match = $var->getLastMatch(); $this->assertTrue(is_array($match) && !empty($match) && count($match) >= 1); unset($var, $match); - $var = new Locator; + $var = new DomainLocator; $var->findWhoisServer("danpock.xyz"); $match = $var->getLastMatch(); $this->assertTrue(is_array($match) && !empty($match) && count($match) >= 1);