|
2 | 2 | // Copyright 1999-2016. Parallels IP Holdings GmbH.
|
3 | 3 |
|
4 | 4 | namespace PleskX\Api\Operator;
|
| 5 | +use PleskX\Api\Struct\Database as Struct; |
5 | 6 |
|
6 | 7 | class Database extends \PleskX\Api\Operator
|
7 | 8 | {
|
| 9 | + /** |
| 10 | + * @param array $properties |
| 11 | + * @return Struct\Info |
| 12 | + */ |
| 13 | + public function create($properties) |
| 14 | + { |
| 15 | + return new Struct\Info($this->_create('add-db', $properties)); |
| 16 | + } |
8 | 17 |
|
| 18 | + /** |
| 19 | + * @param $properties |
| 20 | + * @return Struct\UserInfo |
| 21 | + */ |
| 22 | + public function createUser($properties) |
| 23 | + { |
| 24 | + return new Struct\UserInfo($this->_create('add-db-user', $properties)); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * @param $command |
| 29 | + * @param array $properties |
| 30 | + * @return \PleskX\Api\XmlResponse |
| 31 | + */ |
| 32 | + private function _create($command, array $properties) |
| 33 | + { |
| 34 | + $packet = $this->_client->getPacket(); |
| 35 | + $info = $packet->addChild($this->_wrapperTag)->addChild($command); |
| 36 | + |
| 37 | + foreach ($properties as $name => $value) { |
| 38 | + $info->addChild($name, $value); |
| 39 | + } |
| 40 | + |
| 41 | + return $this->_client->request($packet); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * @param string $field |
| 46 | + * @param integer|string $value |
| 47 | + * @return Struct\Info |
| 48 | + */ |
| 49 | + public function get($field, $value) |
| 50 | + { |
| 51 | + $items = $this->getAll($field, $value); |
| 52 | + return reset($items); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @param string $field |
| 57 | + * @param integer|string $value |
| 58 | + * @return Struct\UserInfo |
| 59 | + */ |
| 60 | + public function getUser($field, $value) |
| 61 | + { |
| 62 | + $items = $this->getAllUsers($field, $value); |
| 63 | + return reset($items); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * @param string $field |
| 68 | + * @param integer|string $value |
| 69 | + * @return Struct\Info[] |
| 70 | + */ |
| 71 | + public function getAll($field, $value) |
| 72 | + { |
| 73 | + $response = $this->_get('get-db', $field, $value); |
| 74 | + $items = []; |
| 75 | + foreach ($response->xpath('//result') as $xmlResult) { |
| 76 | + $items[] = new Struct\Info($xmlResult); |
| 77 | + } |
| 78 | + return $items; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * @param string $field |
| 83 | + * @param integer|string $value |
| 84 | + * @return Struct\UserInfo[] |
| 85 | + */ |
| 86 | + public function getAllUsers($field, $value) |
| 87 | + { |
| 88 | + $response = $this->_get('get-db-users', $field, $value); |
| 89 | + $items = []; |
| 90 | + foreach ($response->xpath('//result') as $xmlResult) { |
| 91 | + $items[] = new Struct\UserInfo($xmlResult); |
| 92 | + } |
| 93 | + return $items; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * @param $command |
| 98 | + * @param $field |
| 99 | + * @param $value |
| 100 | + * @return \PleskX\Api\XmlResponse |
| 101 | + */ |
| 102 | + private function _get($command, $field, $value) |
| 103 | + { |
| 104 | + $packet = $this->_client->getPacket(); |
| 105 | + $getTag = $packet->addChild($this->_wrapperTag)->addChild($command); |
| 106 | + |
| 107 | + $filterTag = $getTag->addChild('filter'); |
| 108 | + if (!is_null($field)) { |
| 109 | + $filterTag->addChild($field, $value); |
| 110 | + } |
| 111 | + |
| 112 | + $response = $this->_client->request($packet, \PleskX\Api\Client::RESPONSE_FULL); |
| 113 | + return $response; |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * @param string $field |
| 118 | + * @param integer|string $value |
| 119 | + * @return bool |
| 120 | + */ |
| 121 | + public function delete($field, $value) |
| 122 | + { |
| 123 | + return $this->_delete($field, $value, 'del-db'); |
| 124 | + } |
| 125 | + |
| 126 | + /** |
| 127 | + * @param string $field |
| 128 | + * @param integer|string $value |
| 129 | + * @return bool |
| 130 | + */ |
| 131 | + public function deleteUser($field, $value) |
| 132 | + { |
| 133 | + return $this->_delete($field, $value, 'del-db-user'); |
| 134 | + } |
9 | 135 | }
|
0 commit comments