Kelunik\Acme\AcmeService::doRegister PHP Méthode

doRegister() private méthode

Registers a new account on the server.
private doRegister ( string $email, string | null $agreement = null ) : Generator
$email string e-mail address for contact
$agreement string | null agreement URI or null if not agreed yet
Résultat Generator coroutine resolved by Amp returning a Registration object
    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);
    }