diff --git a/README.md b/README.md index 648808f..8389357 100644 --- a/README.md +++ b/README.md @@ -230,7 +230,28 @@ Level23\Dynadot\ResultObjects\GetContactResponse\Contact Object [Country] => country ) ``` - + +### Set renew option with `setRenewOption` + +```php +setRenewOption('example.com', 'auto'); + // ... +} catch (Exception $e) { + // ... handle exception +} +``` +The `setRenewOption` let's you set the renewal setting for a domain. Values of for the second +argument ($renewOption) van be "donot", "auto", "reset". The method will return `true` when +the renew option is set successfully. + # FAQ ## I keep getting timeouts! diff --git a/src/Dynadot/DynadotApi.php b/src/Dynadot/DynadotApi.php index a16f7e2..1e634ce 100644 --- a/src/Dynadot/DynadotApi.php +++ b/src/Dynadot/DynadotApi.php @@ -17,6 +17,8 @@ use Level23\Dynadot\Exception\ApiHttpCallFailedException; use Level23\Dynadot\ResultObjects\ListDomainInfoResponse; use Level23\Dynadot\Exception\ApiLimitationExceededException; +use Level23\Dynadot\ResultObjects\RenewOptionResponse\SetRenewOptionHeader; +use Level23\Dynadot\ResultObjects\RenewOptionResponse\SetRenewOptionResponse; /** * Class DynadotApi @@ -570,4 +572,57 @@ public function getContactInfo(int $contactId): GetContactResponse\Contact return $resultData->GetContactContent->Contact; } + + /** + * @throws \Level23\Dynadot\Exception\DynadotApiException + * @throws \Sabre\Xml\ParseException + * @throws \GuzzleHttp\Exception\GuzzleException + * @throws \Level23\Dynadot\Exception\ApiHttpCallFailedException + */ + public function setRenewOption(string $domain, string $renewOption): bool + { + $this->log(LogLevel::INFO, 'Set auto renew for: ' . $domain . ' to: ' . $renewOption); + + $requestData = [ + 'command' => 'set_renew_option', + 'domain' => $domain, + 'renew_option' => $renewOption, + ]; + + // perform the API call + $response = $this->performRawApiCall($requestData); + + $this->log(LogLevel::DEBUG, 'Start parsing result'); + + // start parsing XML data using Sabre + $sabreService = new Service(); + + // map certain values to objects + $sabreService->mapValueObject('{}SetRenewOptionResponse', SetRenewOptionResponse::class); + $sabreService->mapValueObject('{}SetRenewOptionHeader', SetRenewOptionHeader::class); + $sabreService->mapValueObject('{}Response', GeneralResponse\Response::class); + $sabreService->mapValueObject('{}ResponseHeader', GeneralResponse\ResponseHeader::class); + + // parse the data + $resultData = $sabreService->parse($response->getContents()); + + // General error, like incorrect api key + if ($resultData instanceof GeneralResponse\Response) { + $code = $resultData->ResponseHeader->ResponseCode; + if ($code != GeneralResponse\ResponseHeader::RESPONSECODE_OK) { + throw new DynadotApiException($resultData->ResponseHeader->Error); + } + } + + if (!$resultData instanceof SetRenewOptionResponse) { + throw new DynadotApiException('We failed to parse the response'); + } + + $code = $resultData->SetRenewOptionHeader->SuccessCode; + if ($code != SetRenewOptionHeader::SUCCESSCODE_OK) { + throw new DynadotApiException((string)$resultData->SetRenewOptionHeader->Error); + } + + return true; + } } diff --git a/src/Dynadot/ResultObjects/RenewOptionResponse/SetRenewOptionHeader.php b/src/Dynadot/ResultObjects/RenewOptionResponse/SetRenewOptionHeader.php new file mode 100644 index 0000000..b0fd8b9 --- /dev/null +++ b/src/Dynadot/ResultObjects/RenewOptionResponse/SetRenewOptionHeader.php @@ -0,0 +1,24 @@ + + + -1 + failed + + \ No newline at end of file diff --git a/tests/ApiTests/MockHttpResponses/renewOptionInvalidResponse.txt b/tests/ApiTests/MockHttpResponses/renewOptionInvalidResponse.txt new file mode 100644 index 0000000..3cd5099 --- /dev/null +++ b/tests/ApiTests/MockHttpResponses/renewOptionInvalidResponse.txt @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/tests/ApiTests/MockHttpResponses/renewOptionSuccessResponse.txt b/tests/ApiTests/MockHttpResponses/renewOptionSuccessResponse.txt new file mode 100644 index 0000000..2350677 --- /dev/null +++ b/tests/ApiTests/MockHttpResponses/renewOptionSuccessResponse.txt @@ -0,0 +1,6 @@ + + + 0 + success + + \ No newline at end of file diff --git a/tests/ApiTests/SetRenewOptionTest.php b/tests/ApiTests/SetRenewOptionTest.php new file mode 100644 index 0000000..0851f0e --- /dev/null +++ b/tests/ApiTests/SetRenewOptionTest.php @@ -0,0 +1,78 @@ +getMockedResponse('renewOptionFailedResponse.txt'); + + $api->setGuzzleOptions(['handler' => $mockHandler]); + + $this->expectException(DynadotApiException::class); + + $api->setRenewOption('example.com', 'auto'); + } + + public function testInvalidResponse(): void + { + $api = new DynadotApi('_API_KEY_GOES_HERE_'); + + $mockHandler = $this->getMockedResponse('renewOptionInvalidResponse.txt'); + + $api->setGuzzleOptions(['handler' => $mockHandler]); + + $this->expectException(DynadotApiException::class); + $this->expectExceptionMessage('We failed to parse the response'); + + $api->setRenewOption('example.com', 'auto'); + } + + /** + * @throws \GuzzleHttp\Exception\GuzzleException + * @throws \Sabre\Xml\ParseException + * @throws \Level23\Dynadot\Exception\ApiHttpCallFailedException + */ + public function testInvalidGeneralResponse(): void + { + $api = new DynadotApi('_API_KEY_GOES_HERE_'); + + $mockHandler = $this->getMockedResponse('invalidApiKeyResponse.txt'); + + $api->setGuzzleOptions(['handler' => $mockHandler]); + + $this->expectException(DynadotApiException::class); + + $api->setRenewOption('example.com', 'auto'); + } + + /** + * @throws \Level23\Dynadot\Exception\DynadotApiException + * @throws \Sabre\Xml\ParseException + * @throws \GuzzleHttp\Exception\GuzzleException + * @throws \Level23\Dynadot\Exception\ApiHttpCallFailedException + */ + public function testSuccessResponse(): void + { + $api = new DynadotApi('_API_KEY_GOES_HERE_'); + + $mockHandler = $this->getMockedResponse('renewOptionSuccessResponse.txt'); + + $api->setGuzzleOptions(['handler' => $mockHandler]); + + $result = $api->setRenewOption('example.com', 'auto'); + + $this->assertTrue($result); + } +} \ No newline at end of file