Skip to content

Commit

Permalink
New setup for parsing the responses from bNamed
Browse files Browse the repository at this point in the history
  • Loading branch information
Marc committed May 24, 2024
1 parent e026c37 commit b4ade91
Show file tree
Hide file tree
Showing 11 changed files with 125 additions and 121 deletions.
4 changes: 3 additions & 1 deletion .idea/bnamed.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 10 additions & 2 deletions src/BNamed.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace Mvdgeijn\BNamed;

use Mvdgeijn\BNamed\Responses\TLDsResponse;
use App\Tools\Domain;
use Mvdgeijn\BNamed\Responses\TLDAllResponse;

class BNamed implements BNamedInterface
{
Expand Down Expand Up @@ -113,7 +114,7 @@ public function getConfig(): array

public function TLDAll()
{
return $this->connector->get('TLDall');
return $this->connector->get('TLDAll');
}

public function check($domains)
Expand All @@ -125,4 +126,11 @@ public function getReactivatableDomains()
{
return $this->connector->get('GetReactivatableDomains');
}

public function getDomain( string $domainName )
{
$domain = Domain::create( $domainName );

return $this->connector->get('GetDomain', ['SLD' => $domain->getSld(), 'TLD' => $domain->getTld()]);
}
}
19 changes: 10 additions & 9 deletions src/Connector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@

namespace Mvdgeijn\BNamed;

use Mvdgeijn\BNamed\Factories\Response;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Request;
use GuzzleHttp\Psr7\Response as PsrResponse;
use GuzzleHttp\Psr7\Utils;
use Mvdgeijn\BNamed\Exceptions\BNamedException;
use Mvdgeijn\BNamed\Responses\Response;
use Mvdgeijn\BNamed\Transformers\Transformer;

class Connector implements ConnectorInterface
class Connector
{
/**
* @var GuzzleClient The Guzzle client.
Expand Down Expand Up @@ -44,11 +43,10 @@ public function __construct(BNamed $client, ?HandlerStack $guzzleHandlerStack =
* Perform a GET request and return the parsed body as response.
*
* @param string $command
* @param array|null $payload (optional)
*
* @return array The response body.
* @param array|null $params
* @return Response
*/
public function get($command, ?array $params = null): array
public function get( string $command, ?array $params = null): Response
{
$params['command'] = $command;

Expand Down Expand Up @@ -76,7 +74,7 @@ public function post(Transformer $params): array
*
* @return mixed[] The decoded JSON response.
*/
protected function makeCall(string $method, string $command, ?array $params = null): array
protected function makeCall(string $method, string $command, ?array $params = null): Response
{
$params['command'] = $command;

Expand All @@ -95,6 +93,7 @@ protected function makeCall(string $method, string $command, ?array $params = nu
*
* @param PsrResponse $response The call response.
*
* @return mixed
* @throws BNamedException
*/
protected function getResult(PsrResponse $response)
Expand All @@ -104,7 +103,9 @@ protected function getResult(PsrResponse $response)
$xml = simplexml_load_string($body );

if( (int)$xml->ErrorCode == 0 ) {
return Response::parse( $xml->Result->children() );
$class = '\\Mvdgeijn\\BNamed\\Responses\\' . (string)$xml->Command . 'Response';

return new $class( $xml );
} else {
throw new BNamedException("bNamed XML error response code " . $xml->ErrorCode . ": " . $xml->ErrorText );
}
Expand Down
26 changes: 0 additions & 26 deletions src/ConnectorInterface.php

This file was deleted.

47 changes: 0 additions & 47 deletions src/Factories/Response.php

This file was deleted.

9 changes: 4 additions & 5 deletions src/Responses/DNSSecDetailResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,16 @@

use SimpleXMLElement;

class DNSSecDetailResponse implements ResponseInterface
class DNSSecDetailResponse extends Response
{
private int $algorithm;

private string $algorithmString;

public static function parse(SimpleXMLElement $element)
public function __construct(SimpleXMLElement $element)
{
return ( new DNSSecDetailResponse() )
->setAlgorithm( (int)$element->algorith )
->setAlgorithmString( (string)$element->algorithmString );
$this->algorith = (int)$element->algorith;
$this->algorithString = (string)$element->algorithmString;
}

/**
Expand Down
65 changes: 65 additions & 0 deletions src/Responses/GetDomainResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Mvdgeijn\BNamed\Responses;

use SimpleXMLElement;

class GetDomainResponse extends Response
{
public string $sld;

public string $tld;

public string $statusCode;

public string $statusText;

public string $dnsType;

public array $dnsServers;

public array $dnsIpList;

public string $authKey;

public bool $transferLock;

public \Carbon $expiration;

public \Carbon $renewlDeadline;

public \Carbon $registrationISO;

public string $extendType;

public bool $trustee;

public function __construct(SimpleXMLElement $element)
{
$this->tld = (string)$element->Result->TLD;

$this->sld = (string)$element->Result->SLD;

$this->statusCode = (string)$element->Result->StatusCode;

$this->statusText = (string)$element->Result->StatusText;

$this->dnsType = (string)$element->Result->DNSType;

$this->authKey = (string)$element->Result->AuthKey;

$this->trustee = ( (string)$element->Result->Trustee == "true" );

$this->transferLock = ( (string)$element->Result->TransferLock == "true");

$this->expiration = \Carbon::parseFromLocale( (string)$element->Result->Expiration );

$this->renewlDeadline = \Carbon::parseFromLocale( (string)$element->Result->RenewalDeadline );

$this->registrationISO = \Carbon::parseFromLocale( (string)$element->Result->RegistrationISO );

$this->dnsServers = explode(",", (string)$element->Result->DNSList );

$this->dnsIpList = explode( ",", (string)$element->Result->DNSIPList );
}
}
8 changes: 8 additions & 0 deletions src/Responses/Response.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Mvdgeijn\BNamed\Responses;

class Response
{

}
14 changes: 14 additions & 0 deletions src/Responses/TLDAllResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace Mvdgeijn\BNamed\Responses;

class TLDAllResponse extends Response
{
public array $data = [];
public function __construct( \SimpleXMLElement $element )
{
foreach( $element->Result->TLDs->children() as $tld ) {
$this->data[] = new TLDResponse( $tld );
}
}
}
26 changes: 11 additions & 15 deletions src/Responses/TLDResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use SimpleXMLElement;

class TLDResponse implements ResponseInterface
class TLDResponse extends Response
{
public string $tld;

Expand All @@ -24,30 +24,26 @@ class TLDResponse implements ResponseInterface

public array $prices = [];

public static function parse(SimpleXMLElement $element)
public function __construct(SimpleXMLElement $element)
{
$rsp = new TLDResponse();

$rsp->tld = (string)$element->TLD;
$this->tld = (string)$element->TLD;

foreach( $element->DNSSec->children() as $dnssec )
$rsp->dnssec[] = DNSSecDetailResponse::parse( $dnssec );
$this->dnssec[] = new DNSSecDetailResponse( $dnssec );

$rsp->region = explode(",", (string)$element->Regio_EN);
$this->region = explode(",", (string)$element->Regio_EN);

$rsp->registrationPeriod = explode(",", (string)$element->Registration_Period);
$this->registrationPeriod = explode(",", (string)$element->Registration_Period);

$rsp->registrationPeriodAfterTransfer = (string)$element->RegistrationPeriodAfterTransfer;
$this->registrationPeriodAfterTransfer = (string)$element->RegistrationPeriodAfterTransfer;

$rsp->extendPeriod = explode(",", (string)$element->Extend_Period);
$this->extendPeriod = explode(",", (string)$element->Extend_Period);

$rsp->transferPeriod = explode(",", (string)$element->Transfer_Period);
$this->transferPeriod = explode(",", (string)$element->Transfer_Period);

$rsp->minimumLength = (int)$element->Minimum_Length;
$this->minimumLength = (int)$element->Minimum_Length;

foreach( (array)$element->Prices as $key => $value )
$rsp->prices[$key] = str_replace(",", ".", $value );

return $rsp;
$this->prices[$key] = str_replace(",", ".", $value );
}
}
16 changes: 0 additions & 16 deletions src/Responses/TLDsResponse.php

This file was deleted.

0 comments on commit b4ade91

Please sign in to comment.