Skip to content

Commit c3ce3d0

Browse files
committed
Reduce code duplication in delete methods.
1 parent 4680834 commit c3ce3d0

File tree

8 files changed

+25
-32
lines changed

8 files changed

+25
-32
lines changed

src/PleskX/Api/Operator.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ class Operator
1515
public function __construct($client)
1616
{
1717
$this->_client = $client;
18+
19+
if (is_null($this->_wrapperTag)) {
20+
$classNameParts = explode('\\', get_class($this));
21+
$this->_wrapperTag = end($classNameParts);
22+
$this->_wrapperTag = strtolower(preg_replace('/([a-z])([A-Z])/', '\1-\2', $this->_wrapperTag));
23+
}
1824
}
1925

2026
/**
@@ -28,12 +34,6 @@ public function request($request, $mode = Client::RESPONSE_SHORT)
2834
{
2935
$wrapperTag = $this->_wrapperTag;
3036

31-
if (is_null($wrapperTag)) {
32-
$classNameParts = explode('\\', get_class($this));
33-
$wrapperTag = end($classNameParts);
34-
$wrapperTag = strtolower(preg_replace('/([a-z])([A-Z])/', '\1-\2', $wrapperTag));
35-
}
36-
3737
if (is_array($request)) {
3838
$request = [$wrapperTag => $request];
3939
} else if (preg_match('/^[a-z]/', $request)) {
@@ -45,4 +45,16 @@ public function request($request, $mode = Client::RESPONSE_SHORT)
4545
return $this->_client->request($request, $mode);
4646
}
4747

48+
/**
49+
* @param string $field
50+
* @param integer|string $value
51+
* @param string $deleteMethodName
52+
* @return bool
53+
*/
54+
public function _delete($field, $value, $deleteMethodName = 'del')
55+
{
56+
$response = $this->request("$deleteMethodName.filter.$field=$value");
57+
return 'ok' === (string)$response->status;
58+
}
59+
4860
}

src/PleskX/Api/Operator/Customer.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,7 @@ public function create($properties)
3131
*/
3232
public function delete($field, $value)
3333
{
34-
$packet = $this->_client->getPacket();
35-
$packet->addChild('customer')->addChild('del')->addChild('filter')->addChild($field, $value);
36-
$response = $this->_client->request($packet);
37-
return 'ok' === (string)$response->status;
34+
return $this->_delete($field, $value);
3835
}
3936

4037
/**

src/PleskX/Api/Operator/Reseller.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,7 @@ public function create($properties)
3131
*/
3232
public function delete($field, $value)
3333
{
34-
$packet = $this->_client->getPacket();
35-
$packet->addChild('reseller')->addChild('del')->addChild('filter')->addChild($field, $value);
36-
$response = $this->_client->request($packet);
37-
return 'ok' === (string)$response->status;
34+
return $this->_delete($field, $value);
3835
}
3936

4037
/**

src/PleskX/Api/Operator/SecretKey.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,7 @@ public function getInfo($keyId)
3939
*/
4040
public function delete($keyId)
4141
{
42-
$packet = $this->_client->getPacket();
43-
$packet->addChild('secret_key')->addChild('delete')->addChild('filter')->addChild('key', $keyId);
44-
$response = $this->_client->request($packet);
45-
return 'ok' === (string)$response->status;
42+
return $this->_delete('key', $keyId, 'delete');
4643
}
4744

4845
}

src/PleskX/Api/Operator/Site.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,7 @@ public function create(array $properties)
3232
*/
3333
public function delete($field, $value)
3434
{
35-
$packet = $this->_client->getPacket();
36-
$packet->addChild('site')->addChild('del')->addChild('filter')->addChild($field, $value);
37-
$response = $this->_client->request($packet);
38-
return 'ok' === (string)$response->status;
35+
return $this->_delete($field, $value);
3936
}
4037

4138
/**

src/PleskX/Api/Operator/Ui.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ public function getCustomButton($id)
5252
*/
5353
public function deleteCustomButton($id)
5454
{
55-
$response = $this->request("delete-custombutton.filter.custombutton-id=$id");
56-
return 'ok' === (string)$response->status;
55+
return $this->_delete('custombutton-id', $id, 'delete-custombutton');
5756
}
5857

5958
}

src/PleskX/Api/Operator/User.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,7 @@ public function create($role, $properties)
3535
*/
3636
public function delete($field, $value)
3737
{
38-
$packet = $this->_client->getPacket();
39-
$packet->addChild('user')->addChild('del')->addChild('filter')->addChild($field, $value);
40-
$response = $this->_client->request($packet);
41-
return 'ok' === (string)$response->status;
38+
return $this->_delete($field, $value);
4239
}
4340

4441
/**

src/PleskX/Api/Operator/Webspace.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,7 @@ public function create(array $properties, array $hostingProperties = null)
6464
*/
6565
public function delete($field, $value)
6666
{
67-
$packet = $this->_client->getPacket();
68-
$packet->addChild('webspace')->addChild('del')->addChild('filter')->addChild($field, $value);
69-
$response = $this->_client->request($packet);
70-
return 'ok' === (string)$response->status;
67+
return $this->_delete($field, $value);
7168
}
7269

7370
/**

0 commit comments

Comments
 (0)