Completed
Push — master ( 841fdc...5147f3 )
by Alexei
02:10
created

Operator::_getItems()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 21
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 21
rs 9.3142
cc 3
eloc 12
nc 4
nop 4
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
     * @return mixed
66
     */
67
    protected function _getItems($structClass, $infoTag, $field = null, $value = null)
68
    {
69
        $packet = $this->_client->getPacket();
70
        $getTag = $packet->addChild($this->_wrapperTag)->addChild('get');
71
72
        $filterTag = $getTag->addChild('filter');
73
        if (!is_null($field)) {
74
            $filterTag->addChild($field, $value);
75
        }
76
77
        $getTag->addChild('dataset')->addChild($infoTag);
78
79
        $response = $this->_client->request($packet, \PleskX\Api\Client::RESPONSE_FULL);
80
81
        $items = [];
82
        foreach ($response->xpath('//result') as $xmlResult) {
83
            $items[] = new $structClass($xmlResult->data->$infoTag);
84
        }
85
86
        return $items;
87
    }
88
89
}
90