Skip to content

Commit 0ec7b14

Browse files
committed
Reduce amount of code duplication for "get" operations.
1 parent 841fdc0 commit 0ec7b14

File tree

5 files changed

+57
-63
lines changed

5 files changed

+57
-63
lines changed

src/PleskX/Api/Operator.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,33 @@ protected function _delete($field, $value, $deleteMethodName = 'del')
5757
return 'ok' === (string)$response->status;
5858
}
5959

60+
/**
61+
* @param string $structClass
62+
* @param string $infoTag
63+
* @param string|null $field
64+
* @param integer|string|null $value
65+
* @return mixed
66+
*/
67+
protected function _getItems($structClass, $infoTag, $field = null, $value = null)
68+
{
69+
$packet = $this->_client->getPacket();
70+
$getTag = $packet->addChild($this->_wrapperTag)->addChild('get');
71+
72+
$filterTag = $getTag->addChild('filter');
73+
if (!is_null($field)) {
74+
$filterTag->addChild($field, $value);
75+
}
76+
77+
$getTag->addChild('dataset')->addChild($infoTag);
78+
79+
$response = $this->_client->request($packet, \PleskX\Api\Client::RESPONSE_FULL);
80+
81+
$items = [];
82+
foreach ($response->xpath('//result') as $xmlResult) {
83+
$items[] = new $structClass($xmlResult->data->$infoTag);
84+
}
85+
86+
return $items;
87+
}
88+
6089
}

src/PleskX/Api/Operator/Customer.php

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function delete($field, $value)
4141
*/
4242
public function get($field, $value)
4343
{
44-
$items = $this->_get($field, $value);
44+
$items = $this->_getItems(Struct\GeneralInfo::class, 'gen_info', $field, $value);
4545
return reset($items);
4646
}
4747

@@ -50,34 +50,7 @@ public function get($field, $value)
5050
*/
5151
public function getAll()
5252
{
53-
return $this->_get();
54-
}
55-
56-
/**
57-
* @param string|null $field
58-
* @param integer|string|null $value
59-
* @return Struct\GeneralInfo|Struct\GeneralInfo[]
60-
*/
61-
private function _get($field = null, $value = null)
62-
{
63-
$packet = $this->_client->getPacket();
64-
$getTag = $packet->addChild($this->_wrapperTag)->addChild('get');
65-
66-
$filterTag = $getTag->addChild('filter');
67-
if (!is_null($field)) {
68-
$filterTag->addChild($field, $value);
69-
}
70-
71-
$getTag->addChild('dataset')->addChild('gen_info');
72-
73-
$response = $this->_client->request($packet, \PleskX\Api\Client::RESPONSE_FULL);
74-
75-
$customers = [];
76-
foreach ($response->xpath('//result') as $xmlResult) {
77-
$customers[] = new Struct\GeneralInfo($xmlResult->data->gen_info);
78-
}
79-
80-
return $customers;
53+
return $this->_getItems(Struct\GeneralInfo::class, 'gen_info');
8154
}
8255

8356
}

src/PleskX/Api/Operator/Reseller.php

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,26 @@ public function delete($field, $value)
3434
return $this->_delete($field, $value);
3535
}
3636

37+
3738
/**
3839
* @param string $field
3940
* @param integer|string $value
4041
* @return Struct\GeneralInfo
4142
*/
4243
public function get($field, $value)
4344
{
44-
$packet = $this->_client->getPacket();
45-
$getTag = $packet->addChild($this->_wrapperTag)->addChild('get');
46-
$getTag->addChild('filter')->addChild($field, $value);
47-
$getTag->addChild('dataset')->addChild('gen-info');
48-
$response = $this->_client->request($packet);
49-
return new Struct\GeneralInfo($response->data->{'gen-info'});
45+
$items = $this->_getItems(Struct\GeneralInfo::class, 'gen-info', $field, $value);
46+
return reset($items);
47+
}
48+
49+
/**
50+
* @return Struct\GeneralInfo[]
51+
*/
52+
public function getAll()
53+
{
54+
return $this->_getItems(Struct\GeneralInfo::class, 'gen-info');
5055
}
5156

57+
58+
5259
}

src/PleskX/Api/Operator/Site.php

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -42,35 +42,16 @@ public function delete($field, $value)
4242
*/
4343
public function get($field, $value)
4444
{
45-
$items = $this->getAll($field, $value);
45+
$items = $this->_getItems(Struct\GeneralInfo::class, 'gen_info', $field, $value);
4646
return reset($items);
4747
}
4848

4949
/**
50-
* @param string $field
51-
* @param integer|string $value
5250
* @return Struct\GeneralInfo[]
5351
*/
54-
public function getAll($field = null, $value = null)
52+
public function getAll()
5553
{
56-
$packet = $this->_client->getPacket();
57-
$getTag = $packet->addChild($this->_wrapperTag)->addChild('get');
58-
59-
$filterTag = $getTag->addChild('filter');
60-
if (!is_null($field)) {
61-
$filterTag->addChild($field, $value);
62-
}
63-
64-
$getTag->addChild('dataset')->addChild('gen_info');
65-
66-
$response = $this->_client->request($packet, \PleskX\Api\Client::RESPONSE_FULL);
67-
68-
$items = [];
69-
foreach ($response->xpath('//result') as $xmlResult) {
70-
$items[] = new Struct\GeneralInfo($xmlResult->data->gen_info);
71-
}
72-
73-
return $items;
54+
return $this->_getItems(Struct\GeneralInfo::class, 'gen_info');
7455
}
7556

7657
}

src/PleskX/Api/Operator/Webspace.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -74,12 +74,16 @@ public function delete($field, $value)
7474
*/
7575
public function get($field, $value)
7676
{
77-
$packet = $this->_client->getPacket();
78-
$getTag = $packet->addChild($this->_wrapperTag)->addChild('get');
79-
$getTag->addChild('filter')->addChild($field, $value);
80-
$getTag->addChild('dataset')->addChild('gen_info');
81-
$response = $this->_client->request($packet);
82-
return new Struct\GeneralInfo($response->data->gen_info);
77+
$items = $this->_getItems(Struct\GeneralInfo::class, 'gen_info', $field, $value);
78+
return reset($items);
79+
}
80+
81+
/**
82+
* @return Struct\GeneralInfo[]
83+
*/
84+
public function getAll()
85+
{
86+
return $this->_getItems(Struct\GeneralInfo::class, 'gen_info');
8387
}
8488

8589
}

0 commit comments

Comments
 (0)