Completed
Pull Request — master (#26)
by
unknown
02:12
created

Operator   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 88
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 2
A request() 0 14 3
A _delete() 0 5 1
B _getItems() 0 24 5
1
<?php
2
// Copyright 1999-2016. Parallels IP Holdings GmbH.
3
4
namespace PleskX\Api;
5
6
class Operator
7
{
8
9
    /** @var string|null */
10
    protected $_wrapperTag = null;
11
12
    /** @var \PleskX\Api\Client */
13
    protected $_client;
14
15
    public function __construct($client)
16
    {
17
        $this->_client = $client;
18
19
        if (is_null($this->_wrapperTag)) {
20
            $classNameParts = explode('\\', get_class($this));
21
            $this->_wrapperTag = end($classNameParts);
22
            $this->_wrapperTag = strtolower(preg_replace('/([a-z])([A-Z])/', '\1-\2', $this->_wrapperTag));
23
        }
24
    }
25
26
    /**
27
     * Perform plain API request
28
     *
29
     * @param string|array $request
30
     * @param int $mode
31
     * @return XmlResponse
32
     */
33
    public function request($request, $mode = Client::RESPONSE_SHORT)
34
    {
35
        $wrapperTag = $this->_wrapperTag;
36
37
        if (is_array($request)) {
38
            $request = [$wrapperTag => $request];
39
        } else if (preg_match('/^[a-z]/', $request)) {
40
            $request = "$wrapperTag.$request";
41
        } else {
42
            $request = "<$wrapperTag>$request</$wrapperTag>";
43
        }
44
45
        return $this->_client->request($request, $mode);
46
    }
47
48
    /**
49
     * @param string $field
50
     * @param integer|string $value
51
     * @param string $deleteMethodName
52
     * @return bool
53
     */
54
    protected function _delete($field, $value, $deleteMethodName = 'del')
55
    {
56
        $response = $this->request("$deleteMethodName.filter.$field=$value");
57
        return 'ok' === (string)$response->status;
58
    }
59
60
    /**
61
     * @param string $structClass
62
     * @param string $infoTag
63
     * @param string|null $field
64
     * @param integer|string|null $value
65
     * @param callable|null $filter
66
     * @return mixed
67
     */
68
    protected function _getItems($structClass, $infoTag, $field = null, $value = null, callable $filter = null)
69
    {
70
        $packet = $this->_client->getPacket();
71
        $getTag = $packet->addChild($this->_wrapperTag)->addChild('get');
72
73
        $filterTag = $getTag->addChild('filter');
74
        if (!is_null($field)) {
75
            $filterTag->addChild($field, $value);
76
        }
77
78
        $getTag->addChild('dataset')->addChild($infoTag);
79
80
        $response = $this->_client->request($packet, \PleskX\Api\Client::RESPONSE_FULL);
81
82
        $items = [];
83
        foreach ($response->xpath('//result') as $xmlResult) {
84
            if (!is_null($filter) && !$filter($xmlResult->data->$infoTag)) {
85
                continue;
86
            }
87
            $items[] = new $structClass($xmlResult->data->$infoTag);
88
        }
89
90
        return $items;
91
    }
92
93
}
94