Test Failed
Branch develop (a9f1e9)
by Ludwig
02:28
created

ZonesEndpoint::rectify()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the CwdPowerDNS Client
5
 *
6
 * (c) 2018 cwd.at GmbH <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Cwd\PowerDNSClient\Endpoints;
15
16
use Cwd\PowerDNSClient\Model\Zone;
17
18
class ZonesEndpoint extends AbstractEndpoint
19
{
20
    const ENDPOINT_LIST = 'servers/%s/zones';
21
    const ENDPOINT_ELEMENT = 'servers/%s/zones/%s';
22
23
    public function update(Zone $zone, $lazyLoad = false, $hydrationClass = Zone::class): ?Zone
24
    {
25
        $this->validateEntity($zone, ['UPDATE']);
26
27
        //$zone->setRrsets([]);
28
29
        $payload = $this->getClient()->getSerializer()->serialize($zone, 'json', ['groups' => ['REPLACE']]);
30
31
        $this->getClient()->call($payload, sprintf(self::ENDPOINT_ELEMENT, $this->defaultServerId, $zone->getId()), null, false, 'PUT');
32
33
        if ($lazyLoad) {
34
            return $this->get($zone->getId(), $hydrationClass);
35
        }
36
37
        return null;
38
    }
39
40
    public function updateRRSets(Zone $zone, $lazyLoad = false, $hydrationClass = Zone::class): ?Zone
41
    {
42
        $this->validateEntity($zone, ['UPDATE']);
43
44
        // Remove RecordSets which are not changed
45
        $newSet = [];
46
        /** @var Zone\RRSet $rrset */
47
        foreach ($zone->getRrsets() as $rrset) {
48
            if (null !== $rrset->getChangetype()) {
49
                $newSet[] = $rrset;
50
            }
51
        }
52
53
        $payload = $this->getClient()->getSerializer()->serialize(['rrsets' => $newSet], 'json', ['groups' => ['REPLACE']]);
54
55
        $this->getClient()->call($payload, sprintf(self::ENDPOINT_ELEMENT, $this->defaultServerId, $zone->getId()), null, false, 'PATCH');
56
57
        if ($lazyLoad) {
58
            return $this->get($zone->getId(), $hydrationClass);
59
        }
60
61
        return null;
62
    }
63
64
    /**
65
     * @param string|Zone $zoneId
66
     */
67
    public function delete($zoneId)
68
    {
69
        if ($zoneId instanceof Zone) {
70
            $zoneId = $zoneId->getId();
71
        }
72
73
        $this->getClient()->call(null, sprintf(self::ENDPOINT_ELEMENT, $this->defaultServerId, $zoneId), null, false, 'DELETE');
74
    }
75
76
    public function create(Zone $zone, $rrsets = true, $hydrationClass = Zone::class): Zone
77
    {
78
        $rrsets = ($rrsets) ? 'true' : 'false';
79
80
        $this->validateEntity($zone, ['CREATE']);
81
82
        $payload = $this->getClient()->getSerializer()->serialize($zone, 'json', ['groups' => ['CREATE']]);
83
84
        return $this->getClient()->call($payload, sprintf(self::ENDPOINT_LIST, $this->defaultServerId), $hydrationClass, false, 'POST', ['rrsets' => $rrsets]);
85
    }
86
87
    /**
88
     * @param string|Zone $zoneId
89
     *
90
     * @return Zone
91
     *
92
     * @throws \Http\Client\Exception
93
     */
94
    public function get($zoneId, $hydrationClass = Zone::class): Zone
95
    {
96
        if ($zoneId instanceof Zone) {
97
            $zoneId = $zoneId->getId();
98
        }
99
100
        return $this->getClient()->call(null, sprintf(self::ENDPOINT_ELEMENT, $this->defaultServerId, $zoneId), $hydrationClass, false, 'GET');
101
    }
102
103
    /**
104
     * @return Servers[]
105
     *
106
     * @throws \Http\Client\Exception
107
     */
108
    public function all($zoneName = null, $hydrationClass = Zone::class): array
109
    {
110
        $queryParams = [];
111
        if (null !== $zoneName) {
112
            $queryParams['zone'] = $zoneName;
113
        }
114
115
        return $this->getClient()->call(null, sprintf(self::ENDPOINT_LIST, $this->defaultServerId), $hydrationClass, true, 'GET', $queryParams);
116
    }
117
118
    public function axfrRetrieve($zoneId)
119
    {
120
        if ($zoneId instanceof Zone) {
121
            $zoneId = $zoneId->getId();
122
        }
123
124
        $this->getClient()->call(null, sprintf(self::ENDPOINT_ELEMENT, $this->defaultServerId, $zoneId).'/axfr-retrieve', null, false, 'PUT');
125
    }
126
127
    public function notify($zoneId)
128
    {
129
        if ($zoneId instanceof Zone) {
130
            $zoneId = $zoneId->getId();
131
        }
132
133
        $this->getClient()->call(null, sprintf(self::ENDPOINT_ELEMENT, $this->defaultServerId, $zoneId).'/notify', null, false, 'PUT');
134
    }
135
136
    public function export($zoneId)
137
    {
138
        if ($zoneId instanceof Zone) {
139
            $zoneId = $zoneId->getId();
140
        }
141
142
        return $this->getClient()->call(null, sprintf(self::ENDPOINT_ELEMENT, $this->defaultServerId, $zoneId).'/export', null, false, 'GET');
143
    }
144
145
    public function check($zoneId)
146
    {
147
        if ($zoneId instanceof Zone) {
148
            $zoneId = $zoneId->getId();
149
        }
150
151
        return $this->getClient()->call(null, sprintf(self::ENDPOINT_ELEMENT, $this->defaultServerId, $zoneId).'/check', null, false, 'GET');
152
    }
153
154
    public function rectify($zoneId)
155
    {
156
        if ($zoneId instanceof Zone) {
157
            $zoneId = $zoneId->getId();
158
        }
159
160
        return $this->getClient()->call(null, sprintf(self::ENDPOINT_ELEMENT, $this->defaultServerId, $zoneId).'/rectify', null, false, 'PUT');
161
    }
162
}
163