private function doRegister($email, $agreement = null)
{
if (!is_string($email)) {
throw new InvalidArgumentException(sprintf("\$email must be of type string, %s given.", gettype($email)));
}
if ($agreement !== null && !is_string($agreement)) {
throw new InvalidArgumentException(sprintf("\$agreement must be of type string, %s given.", gettype($agreement)));
}
$payload = ["contact" => ["mailto:{$email}"]];
if ($agreement) {
$payload["agreement"] = $agreement;
}
/** @var Response $response */
$response = (yield $this->acmeClient->post(AcmeResource::NEW_REGISTRATION, $payload));
if ($response->getStatus() === 201) {
if (!$response->hasHeader("location")) {
throw new AcmeException("Protocol Violation: No Location Header");
}
list($location) = $response->getHeader("location");
$payload = json_decode($response->getBody());
if ($response->hasHeader("link")) {
$links = $response->getHeader("link");
foreach ($links as $link) {
if (preg_match("#<(.*?)>;rel=\"terms-of-service\"#x", $link, $match)) {
$uri = \Sabre\Uri\resolve($response->getRequest()->getUri(), $match[1]);
(yield new CoroutineResult($this->register($email, $uri)));
return;
}
}
}
$contact = isset($payload->contact) ? $payload->contact : [];
$agreement = isset($payload->agreement) ? $payload->agreement : null;
$authorizations = isset($payload->authorizations) ? $payload->authorizations : [];
$certificates = isset($payload->certificates) ? $payload->certificates : [];
(yield new CoroutineResult(new Registration($location, $contact, $agreement, $authorizations, $certificates)));
}
if ($response->getStatus() === 409) {
if (!$response->hasHeader("location")) {
throw new AcmeException("Protocol violation: 409 Conflict. Response didn't carry any location header.");
}
list($location) = $response->getHeader("location");
$payload = ["resource" => AcmeResource::REGISTRATION, "contact" => ["mailto:{$email}"]];
if ($agreement) {
$payload["agreement"] = $agreement;
}
$response = (yield $this->acmeClient->post($location, $payload));
$payload = json_decode($response->getBody());
if ($response->hasHeader("link")) {
$links = $response->getHeader("link");
foreach ($links as $link) {
if (preg_match("#<(.*?)>;rel=\"terms-of-service\"#x", $link, $match)) {
$uri = \Sabre\Uri\resolve($response->getRequest()->getUri(), $match[1]);
if ($uri !== $agreement) {
(yield new CoroutineResult($this->register($email, $uri)));
return;
}
}
}
}
$contact = isset($payload->contact) ? $payload->contact : [];
$agreement = isset($payload->agreement) ? $payload->agreement : null;
(yield new CoroutineResult(new Registration($location, $contact, $agreement)));
return;
}
throw $this->generateException($response);
}