Database::getAllUsers()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 9
Ratio 90 %

Importance

Changes 0
Metric Value
dl 9
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 2
1
<?php
2
// Copyright 1999-2021. Plesk International GmbH.
3
4
namespace PleskX\Api\Operator;
5
6
use PleskX\Api\Struct\Database as Struct;
7
8
class Database extends \PleskX\Api\Operator
9
{
10
    /**
11
     * @param array $properties
12
     *
13
     * @return Struct\Info
14
     */
15
    public function create($properties)
16
    {
17
        return new Struct\Info($this->_process('add-db', $properties));
18
    }
19
20
    /**
21
     * @param array $properties
22
     *
23
     * @return Struct\UserInfo
24
     */
25
    public function createUser($properties)
26
    {
27
        return new Struct\UserInfo($this->_process('add-db-user', $properties));
28
    }
29
30
    /**
31
     * @param string $command
32
     * @param array $properties
33
     *
34
     * @return \PleskX\Api\XmlResponse
35
     */
36
    private function _process($command, array $properties)
37
    {
38
        $packet = $this->_client->getPacket();
39
        $info = $packet->addChild($this->_wrapperTag)->addChild($command);
40
41
        foreach ($properties as $name => $value) {
42
            if (false !== strpos($value, '&')) {
43
                $info->$name = $value;
44
                continue;
45
            }
46
            $info->addChild($name, $value);
47
        }
48
49
        return $this->_client->request($packet);
50
    }
51
52
    /**
53
     * @param array $properties
54
     *
55
     * @return bool
56
     */
57
    public function updateUser(array $properties)
58
    {
59
        $response = $this->_process('set-db-user', $properties);
60
61
        return 'ok' === (string) $response->status;
62
    }
63
64
    /**
65
     * @param string $field
66
     * @param int|string $value
67
     *
68
     * @return Struct\Info
69
     */
70
    public function get($field, $value)
71
    {
72
        $items = $this->getAll($field, $value);
73
74
        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 74 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...
75
    }
76
77
    /**
78
     * @param string $field
79
     * @param int|string $value
80
     *
81
     * @return Struct\UserInfo
82
     */
83
    public function getUser($field, $value)
84
    {
85
        $items = $this->getAllUsers($field, $value);
86
87
        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 87 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...
88
    }
89
90
    /**
91
     * @param string $field
92
     * @param int|string $value
93
     *
94
     * @return Struct\Info[]
95
     */
96 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...
97
    {
98
        $response = $this->_get('get-db', $field, $value);
99
        $items = [];
100
        foreach ($response->xpath('//result') as $xmlResult) {
101
            $items[] = new Struct\Info($xmlResult);
102
        }
103
104
        return $items;
105
    }
106
107
    /**
108
     * @param string $field
109
     * @param int|string $value
110
     *
111
     * @return Struct\UserInfo[]
112
     */
113 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...
114
    {
115
        $response = $this->_get('get-db-users', $field, $value);
116
        $items = [];
117
        foreach ($response->xpath('//result') as $xmlResult) {
118
            $items[] = new Struct\UserInfo($xmlResult);
119
        }
120
121
        return $items;
122
    }
123
124
    /**
125
     * @param string $command
126
     * @param string $field
127
     * @param int|string $value
128
     *
129
     * @return \PleskX\Api\XmlResponse
130
     */
131 View Code Duplication
    private function _get($command, $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...
132
    {
133
        $packet = $this->_client->getPacket();
134
        $getTag = $packet->addChild($this->_wrapperTag)->addChild($command);
135
136
        $filterTag = $getTag->addChild('filter');
137
        if (!is_null($field)) {
138
            $filterTag->addChild($field, $value);
139
        }
140
141
        $response = $this->_client->request($packet, \PleskX\Api\Client::RESPONSE_FULL);
142
143
        return $response;
144
    }
145
146
    /**
147
     * @param string $field
148
     * @param int|string $value
149
     *
150
     * @return bool
151
     */
152
    public function delete($field, $value)
153
    {
154
        return $this->_delete($field, $value, 'del-db');
155
    }
156
157
    /**
158
     * @param string $field
159
     * @param int|string $value
160
     *
161
     * @return bool
162
     */
163
    public function deleteUser($field, $value)
164
    {
165
        return $this->_delete($field, $value, 'del-db-user');
166
    }
167
}
168