| 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\SearchResult; |
||
| 17 | use Cwd\PowerDNSClient\Model\Zone; |
||
| 18 | |||
| 19 | class ZonesEndpoint extends AbstractEndpoint |
||
| 20 | { |
||
| 21 | protected const ENDPOINT_LIST = 'servers/%s/zones'; |
||
| 22 | protected const ENDPOINT_ELEMENT = 'servers/%s/zones/%s'; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @param Zone $zone |
||
| 26 | * @param bool $lazyLoad |
||
| 27 | * @param string $hydrationClass |
||
| 28 | * |
||
| 29 | * @return Zone|null |
||
| 30 | * |
||
| 31 | * @throws \Http\Client\Exception |
||
| 32 | */ |
||
| 33 | public function update(Zone $zone, $lazyLoad = false, string $hydrationClass = Zone::class): ?Zone |
||
| 34 | { |
||
| 35 | $this->validateEntity($zone, ['UPDATE']); |
||
| 36 | $payload = $this->getClient()->getSerializer()->serialize($zone, 'json', ['groups' => ['REPLACE']]); |
||
| 37 | |||
| 38 | $this->getClient()->call($payload, sprintf(self::ENDPOINT_ELEMENT, $this->defaultServerId, $zone->getId()), null, false, 'PUT'); |
||
| 39 | |||
| 40 | if ($lazyLoad) { |
||
| 41 | return $this->get($zone->getId(), $hydrationClass); |
||
| 42 | } |
||
| 43 | |||
| 44 | return null; |
||
| 45 | } |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @param Zone $zone |
||
| 49 | * @param bool $lazyLoad |
||
| 50 | * @param string $hydrationClass |
||
| 51 | * |
||
| 52 | * @return Zone|null |
||
| 53 | * |
||
| 54 | * @throws \Http\Client\Exception |
||
| 55 | */ |
||
| 56 | public function updateRRSets(Zone $zone, bool $lazyLoad = false, string $hydrationClass = Zone::class): ?Zone |
||
| 57 | { |
||
| 58 | $this->validateEntity($zone, ['UPDATE']); |
||
| 59 | |||
| 60 | // Remove RecordSets which are not changed |
||
| 61 | $newSet = []; |
||
| 62 | /** @var Zone\RRSet $rrset */ |
||
| 63 | foreach ($zone->getRrsets() as $rrset) { |
||
| 64 | if (null !== $rrset->getChangetype()) { |
||
| 65 | $newSet[] = $rrset; |
||
| 66 | } |
||
| 67 | } |
||
| 68 | |||
| 69 | $payload = $this->getClient()->getSerializer()->serialize(['rrsets' => $newSet], 'json', ['groups' => ['REPLACE']]); |
||
| 70 | |||
| 71 | $this->getClient()->call($payload, sprintf(self::ENDPOINT_ELEMENT, $this->defaultServerId, $zone->getId()), null, false, 'PATCH'); |
||
| 72 | |||
| 73 | if ($lazyLoad) { |
||
| 74 | return $this->get($zone->getId(), $hydrationClass); |
||
| 75 | } |
||
| 76 | |||
| 77 | return null; |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @param int|Zone $zoneId |
||
| 82 | * |
||
| 83 | * @throws \Http\Client\Exception |
||
| 84 | */ |
||
| 85 | public function delete($zoneId): void |
||
| 86 | { |
||
| 87 | if ($zoneId instanceof Zone) { |
||
| 88 | $zoneId = $zoneId->getId(); |
||
| 89 | } |
||
| 90 | |||
| 91 | $this->getClient()->call(null, sprintf(self::ENDPOINT_ELEMENT, $this->defaultServerId, $zoneId), null, false, 'DELETE'); |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @param Zone $zone |
||
| 96 | * @param bool $rrsets |
||
| 97 | * @param string $hydrationClass |
||
| 98 | * |
||
| 99 | * @return Zone |
||
| 100 | * |
||
| 101 | * @throws \Http\Client\Exception |
||
| 102 | */ |
||
| 103 | public function create(Zone $zone, $rrsets = true, string $hydrationClass = Zone::class): Zone |
||
| 104 | { |
||
| 105 | $rrsets = $rrsets ? 'true' : 'false'; |
||
|
1 ignored issue
–
show
Unused Code
introduced
by
Loading history...
|
|||
| 106 | |||
| 107 | $this->validateEntity($zone, ['CREATE']); |
||
| 108 | |||
| 109 | $payload = $this->getClient()->getSerializer()->serialize($zone, 'json', ['groups' => ['CREATE']]); |
||
| 110 | |||
| 111 | return $this->getClient()->call($payload, sprintf(self::ENDPOINT_LIST, $this->defaultServerId), $hydrationClass, false, 'POST'); |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @param string|Zone $zoneId |
||
| 116 | * @param string $hydrationClass |
||
| 117 | * |
||
| 118 | * @return Zone |
||
| 119 | * |
||
| 120 | * @throws \Http\Client\Exception |
||
| 121 | */ |
||
| 122 | public function get($zoneId, $hydrationClass = Zone::class): Zone |
||
| 123 | { |
||
| 124 | if ($zoneId instanceof Zone) { |
||
| 125 | $zoneId = $zoneId->getId(); |
||
| 126 | } |
||
| 127 | |||
| 128 | return $this->getClient()->call(null, sprintf(self::ENDPOINT_ELEMENT, $this->defaultServerId, $zoneId), $hydrationClass, false, 'GET'); |
||
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @param string $zoneName |
||
| 133 | * @param string $hydrationClass |
||
| 134 | * |
||
| 135 | * @return Zone[] |
||
| 136 | * |
||
| 137 | * @throws \Http\Client\Exception |
||
| 138 | */ |
||
| 139 | public function all(?string $zoneName = null, string $hydrationClass = Zone::class): array |
||
| 140 | { |
||
| 141 | $queryParams = []; |
||
| 142 | if (null !== $zoneName) { |
||
| 143 | $queryParams['zone[]'] = $zoneName; |
||
| 144 | } |
||
| 145 | |||
| 146 | return $this->getClient()->call(null, sprintf(self::ENDPOINT_LIST, $this->defaultServerId), $hydrationClass, true, 'GET', $queryParams); |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @param int|Zone $zoneId |
||
| 151 | * |
||
| 152 | * @throws \Http\Client\Exception |
||
| 153 | */ |
||
| 154 | public function axfrRetrieve($zoneId): void |
||
| 155 | { |
||
| 156 | if ($zoneId instanceof Zone) { |
||
| 157 | $zoneId = $zoneId->getId(); |
||
| 158 | } |
||
| 159 | |||
| 160 | $this->getClient()->call(null, sprintf(self::ENDPOINT_ELEMENT, $this->defaultServerId, $zoneId).'/axfr-retrieve', null, false, 'PUT'); |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @param int|Zone $zoneId |
||
| 165 | * |
||
| 166 | * @throws \Http\Client\Exception |
||
| 167 | */ |
||
| 168 | public function notify($zoneId): void |
||
| 169 | { |
||
| 170 | if ($zoneId instanceof Zone) { |
||
| 171 | $zoneId = $zoneId->getId(); |
||
| 172 | } |
||
| 173 | |||
| 174 | $this->getClient()->call(null, sprintf(self::ENDPOINT_ELEMENT, $this->defaultServerId, $zoneId).'/notify', null, false, 'PUT'); |
||
| 175 | } |
||
| 176 | |||
| 177 | /** |
||
| 178 | * @param int|Zone $zoneId |
||
| 179 | * |
||
| 180 | * @return string |
||
| 181 | * |
||
| 182 | * @throws \Http\Client\Exception |
||
| 183 | */ |
||
| 184 | public function export($zoneId): ?string |
||
| 185 | { |
||
| 186 | if ($zoneId instanceof Zone) { |
||
| 187 | $zoneId = $zoneId->getId(); |
||
| 188 | } |
||
| 189 | |||
| 190 | return $this->getClient()->call(null, sprintf(self::ENDPOINT_ELEMENT, $this->defaultServerId, $zoneId).'/export', null, false, 'GET'); |
||
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @param int|Zone $zoneId |
||
| 195 | * |
||
| 196 | * @return string |
||
| 197 | * |
||
| 198 | * @throws \Http\Client\Exception |
||
| 199 | */ |
||
| 200 | public function check($zoneId): string |
||
| 201 | { |
||
| 202 | if ($zoneId instanceof Zone) { |
||
| 203 | $zoneId = $zoneId->getId(); |
||
| 204 | } |
||
| 205 | |||
| 206 | return $this->getClient()->call(null, sprintf(self::ENDPOINT_ELEMENT, $this->defaultServerId, $zoneId).'/check', null, false, 'GET'); |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @param int|Zone $zoneId |
||
| 211 | * |
||
| 212 | * @return string |
||
| 213 | * |
||
| 214 | * @throws \Http\Client\Exception |
||
| 215 | */ |
||
| 216 | public function rectify($zoneId): string |
||
| 217 | { |
||
| 218 | if ($zoneId instanceof Zone) { |
||
| 219 | $zoneId = $zoneId->getId(); |
||
| 220 | } |
||
| 221 | |||
| 222 | return $this->getClient()->call(null, sprintf(self::ENDPOINT_ELEMENT, $this->defaultServerId, $zoneId).'/rectify', null, false, 'PUT'); |
||
| 223 | } |
||
| 224 | |||
| 225 | public function search($term, $maxResults = 100) |
||
| 226 | { |
||
| 227 | $queryParams = [ |
||
| 228 | 'q' => $term, |
||
| 229 | 'max' => $maxResults, |
||
| 230 | ]; |
||
| 231 | |||
| 232 | return $this->getClient()->call(null, sprintf('servers/%s/search-data', $this->defaultServerId), SearchResult::class, true, 'GET', $queryParams); |
||
| 233 | } |
||
| 234 | } |
||
| 235 |