Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 61 additions & 1 deletion src/PleskX/Api/Operator/Dns.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,69 @@
<?php
// Copyright 1999-2016. Parallels IP Holdings GmbH.

namespace PleskX\Api\Operator;
use PleskX\Api\Struct\Dns as Struct;

class Dns extends \PleskX\Api\Operator
{
/**
* @param array $properties
* @return Struct\Info
*/
public function create($properties)
{
$packet = $this->_client->getPacket();
$info = $packet->addChild($this->_wrapperTag)->addChild('add_rec');

foreach ($properties as $name => $value) {
$info->addChild($name, $value);
}

return new Struct\Info($this->_client->request($packet));
}

/**
* @param string $field
* @param integer|string $value
* @return Struct\Info
*/
public function get($field, $value)
{
$items = $this->getAll($field, $value);
return reset($items);
}

/**
* @param string $field
* @param integer|string $value
* @return Struct\Info[]
*/
public function getAll($field, $value)
{
$packet = $this->_client->getPacket();
$getTag = $packet->addChild($this->_wrapperTag)->addChild('get_rec');

$filterTag = $getTag->addChild('filter');
if (!is_null($field)) {
$filterTag->addChild($field, $value);
}

$response = $this->_client->request($packet, \PleskX\Api\Client::RESPONSE_FULL);
$items = [];
foreach ($response->xpath('//result') as $xmlResult) {
$item = new Struct\Info($xmlResult->data);
$item->id = (int)$xmlResult->id;
$items[] = $item;
}
return $items;
}

/**
* @param string $field
* @param integer|string $value
* @return bool
*/
public function delete($field, $value)
{
return $this->_delete($field, $value, 'del_rec');
}
}
41 changes: 41 additions & 0 deletions src/PleskX/Api/Struct/Dns/Info.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
// Copyright 1999-2016. Parallels IP Holdings GmbH.

namespace PleskX\Api\Struct\Dns;

class Info extends \PleskX\Api\Struct
{
/** @var integer */
public $id;

/** @var integer */
public $siteId;

/** @var integer */
public $siteAliasId;

/** @var string */
public $type;

/** @var string */
public $host;

/** @var string */
public $value;

/** @var string */
public $opt;

public function __construct($apiResponse)
{
$this->_initScalarProperties($apiResponse, [
'id',
'site-id',
'site-alias-id',
'type',
'host',
'value',
'opt',
]);
}
}
94 changes: 94 additions & 0 deletions tests/DnsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php
// Copyright 1999-2016. Parallels IP Holdings GmbH.

class DnsTest extends TestCase
{
/**
* @var \PleskX\Api\Struct\Webspace\Info
*/
private static $_webspace;

public static function setUpBeforeClass()
{
parent::setUpBeforeClass();
static::$_webspace = static::_createWebspace('example.dom');
}

public static function tearDownAfterClass()
{
parent::tearDownAfterClass();
static::$_client->webspace()->delete('id', static::$_webspace->id);
}

public function testCreate()
{
$dns = static::$_client->dns()->create([
'site-id' => static::$_webspace->id,
'type' => 'TXT',
'host' => 'host',
'value' => 'value'
]);
$this->assertInternalType('integer', $dns->id);
$this->assertGreaterThan(0, $dns->id);
static::$_client->dns()->delete('id', $dns->id);
}

public function testGetById()
{
$dns = static::$_client->dns()->create([
'site-id' => static::$_webspace->id,
'type' => 'TXT',
'host' => '',
'value' => 'value'
]);

$dnsInfo = static::$_client->dns()->get('id', $dns->id);
$this->assertEquals('TXT', $dnsInfo->type);
$this->assertEquals(static::$_webspace->id, $dnsInfo->siteId);
$this->assertEquals('value', $dnsInfo->value);

static::$_client->dns()->delete('id', $dns->id);
}

public function testGetAllByWebspaceId()
{
$dns = static::$_client->dns()->create([
'site-id' => static::$_webspace->id,
'type' => 'DS',
'host' => '',
'value' => '60485 5 1 2BB183AF5F22588179A53B0A98631FAD1A292118'
]);
$dns2 = static::$_client->dns()->create([
'site-id' => static::$_webspace->id,
'type' => 'DS',
'host' => '',
'value' => '60485 5 1 2BB183AF5F22588179A53B0A98631FAD1A292119'
]);
$dnsInfo = static::$_client->dns()->getAll('site-id', static::$_webspace->id);
$dsRecords = [];
foreach ($dnsInfo as $dnsRec) {
if ('DS' == $dnsRec->type ) {
$dsRecords[] = $dnsRec;
}
}
$this->assertEquals(2, count($dsRecords));
foreach ($dsRecords as $dsRecord) {
$this->assertEquals(static::$_webspace->id, $dsRecord->siteId);
}

static::$_client->dns()->delete('id', $dns->id);
static::$_client->dns()->delete('id', $dns2->id);
}

public function testDelete()
{
$dns = static::$_client->dns()->create([
'site-id' => static::$_webspace->id,
'type' => 'TXT',
'host' => 'host',
'value' => 'value'
]);
$result = static::$_client->dns()->delete('id', $dns->id);
$this->assertTrue($result);
}
}