Skip to content

Commit

Permalink
Extracted interface
Browse files Browse the repository at this point in the history
  • Loading branch information
a-v-saitow committed Apr 18, 2018
1 parent 53c9eb1 commit b328791
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 35 deletions.
16 changes: 16 additions & 0 deletions src/BaseClientInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace HadesArchitect\UnitedDomains;

use HadesArchitect\UnitedDomains\Exception\ApiException;

interface BaseClientInterface
{
/**
* @param $method
* @param array $properties
* @throws ApiException
* @return ResponseInterface
*/
public function call($method, array $properties = []): ResponseInterface;
}
22 changes: 18 additions & 4 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@

use GuzzleHttp\ClientInterface;
use GuzzleHttp\Client as HTTPClient;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Uri;
use HadesArchitect\UnitedDomains\Exception\ApiException;
use HadesArchitect\UnitedDomains\Exception\InvalidResponseFormatException;
use HadesArchitect\UnitedDomains\Exception\ServerException;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\UriInterface;

class Client
class Client implements BaseClientInterface
{
/**
* @var ClientInterface
Expand All @@ -23,7 +25,7 @@ class Client
*/
protected $uri;

public function __construct($username, $password)
public function __construct($username, $password, ClientInterface $httpClient = null)
{
$this->uri = Uri::fromParts([
'scheme' => 'https',
Expand All @@ -32,9 +34,16 @@ public function __construct($username, $password)
'query' => sprintf('s_login=%s&s_pw=%s', $username, $password)
]);

$this->httpClient = new HTTPClient();
if (null === $httpClient) {
$httpClient = new HTTPClient();
}

$this->httpClient = $httpClient;
}

/**
* @inheritdoc
*/
public function call($method, array $properties = []): ResponseInterface
{
$query = sprintf(
Expand All @@ -44,7 +53,11 @@ public function call($method, array $properties = []): ResponseInterface
http_build_query($properties)
);

$body = $this->doCall(new Request('get', $this->uri->withQuery($query)));
try {
$body = $this->doCall(new Request('get', $this->uri->withQuery($query)));
} catch (GuzzleException $guzzleException) {
throw new ApiException('An error occured during API call', 0, $guzzleException);
}

$this->validateResponse($body);

Expand All @@ -59,6 +72,7 @@ public function call($method, array $properties = []): ResponseInterface

/**
* @param RequestInterface $request
* @throws GuzzleException
* @return string
*/
protected function doCall(RequestInterface $request): string
Expand Down
44 changes: 13 additions & 31 deletions src/ClientFacade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,10 @@
use HadesArchitect\UnitedDomains\Exception\InvalidParameterException;
use HadesArchitect\UnitedDomains\Exception\ServerException;

class ClientFacade extends TraceableClient
class ClientFacade extends TraceableClient implements ClientInterface
{
/**
* @param string $domain
* @return bool
* @inheritdoc
*/
public function isDomainFree(string $domain): bool
{
Expand All @@ -26,9 +25,7 @@ public function isDomainFree(string $domain): bool
}

/**
* @param string $zone
* @param string $soamname
* @param string $soarname
* @inheritdoc
*/
public function CreateDNSZone(string $zone, string $soamname = '', string $soarname = ''): void
{
Expand All @@ -43,20 +40,15 @@ public function CreateDNSZone(string $zone, string $soamname = '', string $soarn
}

/**
* @param string $zone
* @inheritdoc
*/
public function DeleteDNSZone(string $zone): void
{
$this->call('DeleteDNSZone', ['dnszone' => $zone]);
}

/**
* @param string $zone
* @param string $name
* @param string $type
* @param string $data
* @param int $ttl
* @param string $class
* @inheritdoc
*/
public function addRecord(string $zone, string $name, string $type, string $data, int $ttl = 3600, string $class = 'IN')
{
Expand All @@ -70,9 +62,7 @@ public function addRecord(string $zone, string $name, string $type, string $data
}

/**
* @param string $zone
* @param array $records
* @throws InvalidParameterException
* @inheritdoc
*/
public function addRecords(string $zone, array $records)
{
Expand Down Expand Up @@ -100,11 +90,7 @@ public function addRecords(string $zone, array $records)
}

/**
* @param string $zone
* @param string $name
* @param string $type
* @param string|bool $data
* @param string $class
* @inheritdoc
*/
public function deleteRecord(string $zone, string $name, string $type, $data = false, string $class = 'IN'): void
{
Expand Down Expand Up @@ -134,19 +120,15 @@ function ($record) use ($name) {
}

/**
* @param string $zone
* @return array
* @inheritdoc
*/
public function getRecords(string $zone)
{
return $this->fetchRecords($zone);
}

/**
* @param string $zone
* @param string $type
*
* @return array
* @inheritdoc
*/
public function findRecordsByType(string $zone, string $type)
{
Expand All @@ -171,11 +153,11 @@ function ($record) {
preg_match_all("/(\S+)\s(\d+)\s(\S+)\s(\S+)\s(.*)/", $record, $matches);

return [
'name' => $matches[1][0],
'ttl' => (int)$matches[2][0],
'name' => $matches[1][0],
'ttl' => (int)$matches[2][0],
'class' => $matches[3][0],
'type' => $matches[4][0],
'data' => $matches[5][0]
'type' => $matches[4][0],
'data' => $matches[5][0]
];
},
$this->call('QueryDNSZoneRRList', ['dnszone' => $zone])->getProperty('rr')
Expand Down
66 changes: 66 additions & 0 deletions src/ClientInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace HadesArchitect\UnitedDomains;

use HadesArchitect\UnitedDomains\Exception\InvalidParameterException;

interface ClientInterface extends BaseClientInterface
{
/**
* @param string $domain
* @return bool
*/
public function isDomainFree(string $domain): bool;

/**
* @param string $zone
* @param string $soamname
* @param string $soarname
*/
public function CreateDNSZone(string $zone, string $soamname = '', string $soarname = ''): void;

/**
* @param string $zone
*/
public function DeleteDNSZone(string $zone): void;

/**
* @param string $zone
* @param string $name
* @param string $type
* @param string $data
* @param int $ttl
* @param string $class
*/
public function addRecord(string $zone, string $name, string $type, string $data, int $ttl = 3600, string $class = 'IN');

/**
* @param string $zone
* @param array $records
* @throws InvalidParameterException
*/
public function addRecords(string $zone, array $records);

/**
* @param string $zone
* @param string $name
* @param string $type
* @param string|bool $data
* @param string $class
*/
public function deleteRecord(string $zone, string $name, string $type, $data = false, string $class = 'IN'): void;

/**
* @param string $zone
* @return array
*/
public function getRecords(string $zone);

/**
* @param string $zone
* @param string $type
*
* @return array
*/
public function findRecordsByType(string $zone, string $type);
}

0 comments on commit b328791

Please sign in to comment.