Completed
Push — master ( 17409b...2df08d )
by Alexei
8s
created

Database::createUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
// Copyright 1999-2016. Parallels IP Holdings GmbH.
3
4
namespace PleskX\Api\Operator;
5
use PleskX\Api\Struct\Database as Struct;
6
7
class Database extends \PleskX\Api\Operator
8
{
9
    /**
10
     * @param array $properties
11
     * @return Struct\Info
12
     */
13
    public function create($properties)
14
    {
15
        return new Struct\Info($this->_process('add-db', $properties));
16
    }
17
18
    /**
19
     * @param $properties
20
     * @return Struct\UserInfo
21
     */
22
    public function createUser($properties)
23
    {
24
        return new Struct\UserInfo($this->_process('add-db-user', $properties));
25
    }
26
27
    /**
28
     * @param $command
29
     * @param array $properties
30
     * @return \PleskX\Api\XmlResponse
31
     */
32
    private function _process($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
            if (false !== strpos($value, '&')) {
39
                $info->$name = $value;
40
                continue;
41
            }
42
            $info->addChild($name, $value);
43
        }
44
45
        return $this->_client->request($packet);
46
    }
47
48
    /**
49
     * @param array $properties
50
     * @return bool
51
     */
52
    public function updateUser(array $properties)
53
    {
54
        $response = $this->_process('set-db-user', $properties);
55
        return 'ok' === (string)$response->status;
56
    }
57
58
    /**
59
     * @param string $field
60
     * @param integer|string $value
61
     * @return Struct\Info
62
     */
63
    public function get($field, $value)
64
    {
65
        $items = $this->getAll($field, $value);
66
        return reset($items);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression reset($items); of type PleskX\Api\Struct\Database\Info|false adds false to the return on line 66 which is incompatible with the return type documented by PleskX\Api\Operator\Database::get of type PleskX\Api\Struct\Database\Info. It seems like you forgot to handle an error condition.
Loading history...
67
    }
68
69
    /**
70
     * @param string $field
71
     * @param integer|string $value
72
     * @return Struct\UserInfo
73
     */
74
    public function getUser($field, $value)
75
    {
76
        $items = $this->getAllUsers($field, $value);
77
        return reset($items);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression reset($items); of type PleskX\Api\Struct\Database\UserInfo|false adds false to the return on line 77 which is incompatible with the return type documented by PleskX\Api\Operator\Database::getUser of type PleskX\Api\Struct\Database\UserInfo. It seems like you forgot to handle an error condition.
Loading history...
78
    }
79
80
    /**
81
     * @param string $field
82
     * @param integer|string $value
83
     * @return Struct\Info[]
84
     */
85 View Code Duplication
    public function getAll($field, $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
86
    {
87
        $response = $this->_get('get-db', $field, $value);
88
        $items = [];
89
        foreach ($response->xpath('//result') as $xmlResult) {
90
            $items[] = new Struct\Info($xmlResult);
91
        }
92
        return $items;
93
    }
94
95
    /**
96
     * @param string $field
97
     * @param integer|string $value
98
     * @return Struct\UserInfo[]
99
     */
100 View Code Duplication
    public function getAllUsers($field, $value)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
101
    {
102
        $response = $this->_get('get-db-users', $field, $value);
103
        $items = [];
104
        foreach ($response->xpath('//result') as $xmlResult) {
105
            $items[] = new Struct\UserInfo($xmlResult);
106
        }
107
        return $items;
108
    }
109
110
    /**
111
     * @param $command
112
     * @param $field
113
     * @param $value
114
     * @return \PleskX\Api\XmlResponse
115
     */
116
    private function _get($command, $field, $value)
117
    {
118
        $packet = $this->_client->getPacket();
119
        $getTag = $packet->addChild($this->_wrapperTag)->addChild($command);
120
121
        $filterTag = $getTag->addChild('filter');
122
        if (!is_null($field)) {
123
            $filterTag->addChild($field, $value);
124
        }
125
126
        $response = $this->_client->request($packet, \PleskX\Api\Client::RESPONSE_FULL);
127
        return $response;
128
    }
129
130
    /**
131
     * @param string $field
132
     * @param integer|string $value
133
     * @return bool
134
     */
135
    public function delete($field, $value)
136
    {
137
        return $this->_delete($field, $value, 'del-db');
138
    }
139
140
    /**
141
     * @param string $field
142
     * @param integer|string $value
143
     * @return bool
144
     */
145
    public function deleteUser($field, $value)
146
    {
147
        return $this->_delete($field, $value, 'del-db-user');
148
    }
149
}
150