Completed
Pull Request — master (#86)
by
unknown
02:11
created

Dns::delete()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
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
     * @param array $records
30
     * @return \PleskX\Api\XmlResponse[]
31
     */
32
    public function bulkCreate(array $records)
33
    {
34
        $packet = $this->_client->getPacket();
35
36
        foreach ($records as $properties) {
37
            $info = $packet->addChild($this->_wrapperTag)->addChild('add_rec');
38
39
            foreach ($properties as $name => $value) {
40
                $info->addChild($name, $value);
41
            }
42
        }
43
44
        $response = $this->_client->request($packet, \PleskX\Api\Client::RESPONSE_FULL);
45
        $items = [];
46
        foreach ($response->xpath('//result') as $xmlResult) {
47
            $items[] = $xmlResult;
48
        }
49
50
        return $items;
51
    }
52
53
    /**
54
     * @param string $field
55
     * @param int|string $value
56
     *
57
     * @return Struct\Info
58
     */
59
    public function get($field, $value)
60
    {
61
        $items = $this->getAll($field, $value);
62
63
        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 63 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...
64
    }
65
66
    /**
67
     * @param string $field
68
     * @param int|string $value
69
     *
70
     * @return Struct\Info[]
71
     */
72
    public function getAll($field, $value)
73
    {
74
        $packet = $this->_client->getPacket();
75
        $getTag = $packet->addChild($this->_wrapperTag)->addChild('get_rec');
76
77
        $filterTag = $getTag->addChild('filter');
78
        if (!is_null($field)) {
79
            $filterTag->addChild($field, $value);
80
        }
81
82
        $response = $this->_client->request($packet, \PleskX\Api\Client::RESPONSE_FULL);
83
        $items = [];
84
        foreach ($response->xpath('//result') as $xmlResult) {
85
            $item = new Struct\Info($xmlResult->data);
86
            $item->id = (int) $xmlResult->id;
87
            $items[] = $item;
88
        }
89
90
        return $items;
91
    }
92
93
    /**
94
     * @param string $field
95
     * @param int|string $value
96
     *
97
     * @return bool
98
     */
99
    public function delete($field, $value)
100
    {
101
        return $this->_delete($field, $value, 'del_rec');
102
    }
103
104
    /**
105
     * Delete multiply records by one request.
106
     * @param array $recordIds
107
     * @return \PleskX\Api\XmlResponse[]
108
     */
109
    public function bulkDelete(array $recordIds)
110
    {
111
        $packet = $this->_client->getPacket();
112
113
        foreach ($recordIds as $recordId) {
114
            $packet->addChild($this->_wrapperTag)->addChild('del_rec')
115
                ->addChild('filter')->addChild('id', $recordId);
116
        }
117
118
        $response = $this->_client->request($packet, \PleskX\Api\Client::RESPONSE_FULL);
119
        $items = [];
120
        foreach ($response->xpath('//result') as $xmlResult) {
121
            $items[] = $xmlResult;
122
        }
123
124
        return $items;
125
    }
126
}
127