Skip to content

Commit

Permalink
Merge branch 'new-locator'
Browse files Browse the repository at this point in the history
  • Loading branch information
mallardduck committed May 4, 2018
2 parents 75085c6 + 919da69 commit 10d4ad1
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 54 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ 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.
- 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!

### Removed
- Cleaned up some more PHP 5.6 support code from tests.
- Removed old style locator class.

## [0.1.5] - 2018-05-01
### Removed
Expand Down
9 changes: 5 additions & 4 deletions src/AbstractClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -21,9 +22,9 @@ class AbstractClient

/**
* The TLD Whois locator class.
* @var Locator
* @var AbstractLocator
*/
protected $tldLocator;
protected $whoisLocator;

/**
* The Unicode for IDNA.
Expand Down Expand Up @@ -55,7 +56,7 @@ class AbstractClient
public function __construct()
{
$this->punycode = new Punycode();
$this->tldLocator = new Locator();
$this->whoisLocator = new DomainLocator();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php
namespace MallardDuck\Whois\WhoisServerList;

use MallardDuck\Whois\Exceptions\MissingArgException;
use MallardDuck\Whois\Exceptions\UnknownWhoisException;

/**
Expand All @@ -15,7 +14,7 @@
*
* @version 1.0.0
*/
class Locator
abstract class AbstractLocator
{

/**
Expand All @@ -30,33 +29,33 @@ class Locator
*
* @var string
*/
private $tldListPath = __DIR__ . '/../../blobs/tld.json';
protected $whoisListPath;

/**
* A collection of the TLDs and whois server list.
*
* @var \Tightenco\Collect\Support\Collection
*/
private $tldCollection;
protected $whoisCollection;

/**
* The results of the last looked up domain.
*
* @var array
*/
private $lastMatch;
protected $lastMatch;

/**
* Build the TLD Whois Server Locator class.
*/
public function __construct()
{
$fileData = file_get_contents($this->tldListPath);
$fileData = file_get_contents($this->whoisListPath);
$tldData = json_decode($fileData);
if (null !== $tldData && json_last_error() === JSON_ERROR_NONE) {
$this->loadStatus = true;
}
$this->tldCollection = collect((array) $tldData);
$this->whoisCollection = collect((array) $tldData);
}

/**
Expand Down Expand Up @@ -86,22 +85,7 @@ public function getLastMatch()
*
* @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;
}
abstract public function findWhoisServer($domain);

/**
* Get the Whois server of the domain provided, or previously found domain.
Expand All @@ -117,7 +101,7 @@ public function getWhoisServer($domain = '')
}
$server = current($this->lastMatch);
if ('UNKNOWN' === strtoupper($server)) {
throw new UnknownWhoisException("This domain doesn't have a valid TLD whois server.");
throw new UnknownWhoisException("This domain doesn't have a valid whois server.");
}

return $server;
Expand Down
51 changes: 51 additions & 0 deletions src/WhoisServerList/DomainLocator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
namespace MallardDuck\Whois\WhoisServerList;

use MallardDuck\Whois\Exceptions\MissingArgException;
use MallardDuck\Whois\Exceptions\UnknownWhoisException;

/**
* Whois Server List Locator Class
*
* This class loads a TLD whois list and allows for easy look up.
*
* @author mallardduck <[email protected]>
*
* @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;
}
}
14 changes: 6 additions & 8 deletions tests/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -14,15 +13,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;
}

/**
Expand Down
18 changes: 9 additions & 9 deletions tests/WhoisLocatorExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -24,7 +24,7 @@ public function testBlankStringThrowsException()
{
$this->expectException(MissingArgException::class);

$var = new Locator;
$var = new DomainLocator;
$results = $var->findWhoisServer('');
unset($var, $results);
}
Expand All @@ -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);
Expand All @@ -56,7 +56,7 @@ public function testNullStringThrowsException()
$this->expectException(\Exception::class);
}

$var = new Locator;
$var = new DomainLocator;
$results = $var->findWhoisServer(null);
unset($var, $results);
}
Expand All @@ -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);
Expand All @@ -86,15 +86,15 @@ 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);
unset($var, $results);

$this->expectException(MissingArgException::class);

$var = new Locator;
$var = new DomainLocator;
$results = $var->getWhoisServer();
unset($var, $results);
}
Expand All @@ -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));
Expand All @@ -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');
Expand Down
6 changes: 3 additions & 3 deletions tests/WhoisLocatorLookupsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand All @@ -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);
Expand Down
10 changes: 5 additions & 5 deletions tests/WhoisLocatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,7 +20,7 @@ class WhoisLocatorTest extends TestCase
*/
public function testIsThereAnySyntaxError()
{
$var = new Locator;
$var = new DomainLocator;
$this->assertTrue(is_object($var));
unset($var);
}
Expand All @@ -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);
}
Expand All @@ -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);
Expand Down

0 comments on commit 10d4ad1

Please sign in to comment.