Completed
Push — master ( 6aa3fc...c7d0c1 )
by Alexei
9s
created

Database   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 129
Duplicated Lines 13.95 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 14
c 1
b 0
f 0
lcom 1
cbo 5
dl 18
loc 129
rs 10

10 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 4 1
A createUser() 0 4 1
A _create() 0 11 2
A get() 0 5 1
A getUser() 0 5 1
A getAll() 9 9 2
A getAllUsers() 9 9 2
A _get() 0 13 2
A delete() 0 4 1
A deleteUser() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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->_create('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->_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);
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 52 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...
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);
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 63 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...
64
    }
65
66
    /**
67
     * @param string $field
68
     * @param integer|string $value
69
     * @return Struct\Info[]
70
     */
71 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...
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 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...
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
    }
135
}
136