Completed
Push — master ( ff29e2...548de8 )
by Alexei
02:35
created

Customer   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 5
dl 0
loc 79
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A create() 0 12 2
A delete() 0 7 1
A get() 0 4 1
A getAll() 0 4 1
A _get() 0 21 4
1
<?php
2
// Copyright 1999-2016. Parallels IP Holdings GmbH.
3
4
namespace PleskX\Api\Operator;
5
use PleskX\Api\Struct\Customer as Struct;
6
7
class Customer extends \PleskX\Api\Operator
8
{
9
10
    /**
11
     * @param array $properties
12
     * @return Struct\Info
13
     */
14
    public function create($properties)
15
    {
16
        $packet = $this->_client->getPacket();
17
        $info = $packet->addChild('customer')->addChild('add')->addChild('gen_info');
18
19
        foreach ($properties as $name => $value) {
20
            $info->addChild($name, $value);
21
        }
22
23
        $response = $this->_client->request($packet);
24
        return new Struct\Info($response);
25
    }
26
27
    /**
28
     * @param string $field
29
     * @param integer|string $value
30
     * @return bool
31
     */
32
    public function delete($field, $value)
33
    {
34
        $packet = $this->_client->getPacket();
35
        $packet->addChild('customer')->addChild('del')->addChild('filter')->addChild($field, $value);
36
        $response = $this->_client->request($packet);
37
        return 'ok' === (string)$response->status;
38
    }
39
40
    /**
41
     * @param string $field
42
     * @param integer|string $value
43
     * @return Struct\GeneralInfo
44
     */
45
    public function get($field, $value)
46
    {
47
        return $this->_get($field, $value);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->_get($field, $value); of type PleskX\Api\Struct\Custom...\Customer\GeneralInfo[] adds the type PleskX\Api\Struct\Customer\GeneralInfo[] to the return on line 47 which is incompatible with the return type documented by PleskX\Api\Operator\Customer::get of type PleskX\Api\Struct\Customer\GeneralInfo.
Loading history...
48
    }
49
50
    /**
51
     * @return Struct\GeneralInfo[]
52
     */
53
    public function getAll()
54
    {
55
        return $this->_get();
56
    }
57
58
    /**
59
     * @param string|null $field
60
     * @param integer|string|null $value
61
     * @return Struct\GeneralInfo|Struct\GeneralInfo[]
62
     */
63
    private function _get($field = null, $value = null)
64
    {
65
        $packet = $this->_client->getPacket();
66
        $getTag = $packet->addChild('customer')->addChild('get');
67
68
        $filterTag = $getTag->addChild('filter');
69
        if ($field) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $field of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
70
            $filterTag->addChild($field, $value);
71
        }
72
73
        $getTag->addChild('dataset')->addChild('gen_info');
74
75
        $response = $this->_client->request($packet, \PleskX\Api\Client::RESPONSE_FULL);
76
77
        $customers = [];
78
        foreach ($response->xpath('//result') as $xmlResult) {
79
            $customers[] = new Struct\GeneralInfo($xmlResult->data->gen_info);
80
        }
81
82
        return $field ? reset($customers) : $customers;
83
    }
84
85
}
86