Completed
Pull Request — master (#17)
by
unknown
02:21
created

Database::getAllUsers()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 9
loc 9
rs 9.6666
cc 2
eloc 6
nc 2
nop 2
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
            $info->addChild($name, $value);
39
        }
40
41
        return $this->_client->request($packet);
42
    }
43
44
    /**
45
     * @param array $properties
46
     * @return bool
47
     */
48
    public function updateUser(array $properties)
49
    {
50
        $response = $this->_process('set-db-user', $properties);
51
        return 'ok' === (string)$response->status;
52
    }
53
54
    /**
55
     * @param string $field
56
     * @param integer|string $value
57
     * @return Struct\Info
58
     */
59
    public function get($field, $value)
60
    {
61
        $items = $this->getAll($field, $value);
62
        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 62 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...
63
    }
64
65
    /**
66
     * @param string $field
67
     * @param integer|string $value
68
     * @return Struct\UserInfo
69
     */
70
    public function getUser($field, $value)
71
    {
72
        $items = $this->getAllUsers($field, $value);
73
        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 73 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...
74
    }
75
76
    /**
77
     * @param string $field
78
     * @param integer|string $value
79
     * @return Struct\Info[]
80
     */
81 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...
82
    {
83
        $response = $this->_get('get-db', $field, $value);
84
        $items = [];
85
        foreach ($response->xpath('//result') as $xmlResult) {
86
            $items[] = new Struct\Info($xmlResult);
87
        }
88
        return $items;
89
    }
90
91
    /**
92
     * @param string $field
93
     * @param integer|string $value
94
     * @return Struct\UserInfo[]
95
     */
96 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...
97
    {
98
        $response = $this->_get('get-db-users', $field, $value);
99
        $items = [];
100
        foreach ($response->xpath('//result') as $xmlResult) {
101
            $items[] = new Struct\UserInfo($xmlResult);
102
        }
103
        return $items;
104
    }
105
106
    /**
107
     * @param $command
108
     * @param $field
109
     * @param $value
110
     * @return \PleskX\Api\XmlResponse
111
     */
112
    private function _get($command, $field, $value)
113
    {
114
        $packet = $this->_client->getPacket();
115
        $getTag = $packet->addChild($this->_wrapperTag)->addChild($command);
116
117
        $filterTag = $getTag->addChild('filter');
118
        if (!is_null($field)) {
119
            $filterTag->addChild($field, $value);
120
        }
121
122
        $response = $this->_client->request($packet, \PleskX\Api\Client::RESPONSE_FULL);
123
        return $response;
124
    }
125
126
    /**
127
     * @param string $field
128
     * @param integer|string $value
129
     * @return bool
130
     */
131
    public function delete($field, $value)
132
    {
133
        return $this->_delete($field, $value, 'del-db');
134
    }
135
136
    /**
137
     * @param string $field
138
     * @param integer|string $value
139
     * @return bool
140
     */
141
    public function deleteUser($field, $value)
142
    {
143
        return $this->_delete($field, $value, 'del-db-user');
144
    }
145
}
146