Skip to content

Commit a5e0adc

Browse files
committed
Issue #11: implement mailnames fetching operations
1 parent 2188d01 commit a5e0adc

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

src/Api/Operator/Mail.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,47 @@ public function delete($field, $value, $siteId)
5353

5454
return 'ok' === (string) $response->status;
5555
}
56+
57+
/**
58+
* @param string $name
59+
* @param int $siteId
60+
*
61+
* @return Struct\GeneralInfo
62+
*/
63+
public function get($name, $siteId)
64+
{
65+
$items = $this->getAll($siteId, $name);
66+
67+
return reset($items);
68+
}
69+
70+
/**
71+
* @param int $siteId
72+
* @param string|null $name
73+
*
74+
* @return Struct\GeneralInfo[]
75+
*/
76+
public function getAll($siteId, $name = null)
77+
{
78+
$packet = $this->_client->getPacket();
79+
$getTag = $packet->addChild($this->_wrapperTag)->addChild('get_info');
80+
81+
$filterTag = $getTag->addChild('filter');
82+
$filterTag->addChild('site-id', $siteId);
83+
if (!is_null($name)) {
84+
$filterTag->addChild('name', $name);
85+
}
86+
87+
$response = $this->_client->request($packet, \PleskX\Api\Client::RESPONSE_FULL);
88+
$items = [];
89+
foreach ($response->xpath('//result') as $xmlResult) {
90+
if (!isset($xmlResult->mailname)) {
91+
continue;
92+
}
93+
$item = new Struct\GeneralInfo($xmlResult->mailname);
94+
$items[] = $item;
95+
}
96+
97+
return $items;
98+
}
5699
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
// Copyright 1999-2021. Plesk International GmbH.
3+
4+
namespace PleskX\Api\Struct\Mail;
5+
6+
class GeneralInfo extends \PleskX\Api\Struct
7+
{
8+
/** @var int */
9+
public $id;
10+
11+
/** @var string */
12+
public $name;
13+
14+
/** @var string */
15+
public $description;
16+
17+
public function __construct($apiResponse)
18+
{
19+
$this->_initScalarProperties($apiResponse, [
20+
'id',
21+
'name',
22+
'description',
23+
]);
24+
}
25+
}

tests/MailTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,31 @@ public function testDelete()
5454
$result = static::$_client->mail()->delete('name', $mailname->name, static::$webspace->id);
5555
$this->assertTrue($result);
5656
}
57+
58+
public function testGet()
59+
{
60+
$mailname = static::$_client->mail()->create('test', static::$webspace->id);
61+
62+
$mailnameInfo = static::$_client->mail()->get('test', static::$webspace->id);
63+
$this->assertEquals('test', $mailnameInfo->name);
64+
65+
static::$_client->mail()->delete('name', $mailname->name, static::$webspace->id);
66+
}
67+
68+
public function testGetAll()
69+
{
70+
$mailname = static::$_client->mail()->create('test', static::$webspace->id);
71+
72+
$mailnamesInfo = static::$_client->mail()->getAll(static::$webspace->id);
73+
$this->assertCount(1, $mailnamesInfo);
74+
$this->assertEquals('test', $mailnamesInfo[0]->name);
75+
76+
static::$_client->mail()->delete('name', $mailname->name, static::$webspace->id);
77+
}
78+
79+
public function testGetAllWithoutMailnames()
80+
{
81+
$mailnamesInfo = static::$_client->mail()->getAll(static::$webspace->id);
82+
$this->assertCount(0, $mailnamesInfo);
83+
}
5784
}

0 commit comments

Comments
 (0)