Completed
Push — master ( 328543...824e88 )
by Alexei
25s
created

Dns::bulkDelete()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
cc 3
nc 4
nop 1
1
<?php
2
// Copyright 1999-2020. Plesk International GmbH.
3
4
namespace PleskX\Api\Operator;
5
6
use PleskX\Api\Struct\Dns as Struct;
7
8
class Dns extends \PleskX\Api\Operator
9
{
10
    /**
11
     * @param array $properties
12
     *
13
     * @return Struct\Info
14
     */
15
    public function create($properties)
16
    {
17
        $packet = $this->_client->getPacket();
18
        $info = $packet->addChild($this->_wrapperTag)->addChild('add_rec');
19
20
        foreach ($properties as $name => $value) {
21
            $info->addChild($name, $value);
22
        }
23
24
        return new Struct\Info($this->_client->request($packet));
25
    }
26
27
    /**
28
     * Send multiply records by one request.
29
     *
30
     * @param array $records
31
     *
32
     * @return \PleskX\Api\XmlResponse[]
33
     */
34
    public function bulkCreate(array $records)
35
    {
36
        $packet = $this->_client->getPacket();
37
38
        foreach ($records as $properties) {
39
            $info = $packet->addChild($this->_wrapperTag)->addChild('add_rec');
40
41
            foreach ($properties as $name => $value) {
42
                $info->addChild($name, $value);
43
            }
44
        }
45
46
        $response = $this->_client->request($packet, \PleskX\Api\Client::RESPONSE_FULL);
47
        $items = [];
48
        foreach ($response->xpath('//result') as $xmlResult) {
49
            $items[] = $xmlResult;
50
        }
51
52
        return $items;
53
    }
54
55
    /**
56
     * @param string $field
57
     * @param int|string $value
58
     *
59
     * @return Struct\Info
60
     */
61
    public function get($field, $value)
62
    {
63
        $items = $this->getAll($field, $value);
64
65
        return reset($items);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The expression reset($items); of type PleskX\Api\Struct\Dns\Info|false adds false to the return on line 65 which is incompatible with the return type documented by PleskX\Api\Operator\Dns::get of type PleskX\Api\Struct\Dns\Info. It seems like you forgot to handle an error condition.
Loading history...
66
    }
67
68
    /**
69
     * @param string $field
70
     * @param int|string $value
71
     *
72
     * @return Struct\Info[]
73
     */
74
    public function getAll($field, $value)
75
    {
76
        $packet = $this->_client->getPacket();
77
        $getTag = $packet->addChild($this->_wrapperTag)->addChild('get_rec');
78
79
        $filterTag = $getTag->addChild('filter');
80
        if (!is_null($field)) {
81
            $filterTag->addChild($field, $value);
82
        }
83
84
        $response = $this->_client->request($packet, \PleskX\Api\Client::RESPONSE_FULL);
85
        $items = [];
86
        foreach ($response->xpath('//result') as $xmlResult) {
87
            $item = new Struct\Info($xmlResult->data);
88
            $item->id = (int) $xmlResult->id;
89
            $items[] = $item;
90
        }
91
92
        return $items;
93
    }
94
95
    /**
96
     * @param string $field
97
     * @param int|string $value
98
     *
99
     * @return bool
100
     */
101
    public function delete($field, $value)
102
    {
103
        return $this->_delete($field, $value, 'del_rec');
104
    }
105
106
    /**
107
     * Delete multiply records by one request.
108
     *
109
     * @param array $recordIds
110
     *
111
     * @return \PleskX\Api\XmlResponse[]
112
     */
113
    public function bulkDelete(array $recordIds)
114
    {
115
        $packet = $this->_client->getPacket();
116
117
        foreach ($recordIds as $recordId) {
118
            $packet->addChild($this->_wrapperTag)->addChild('del_rec')
119
                ->addChild('filter')->addChild('id', $recordId);
120
        }
121
122
        $response = $this->_client->request($packet, \PleskX\Api\Client::RESPONSE_FULL);
123
        $items = [];
124
        foreach ($response->xpath('//result') as $xmlResult) {
125
            $items[] = $xmlResult;
126
        }
127
128
        return $items;
129
    }
130
}
131