@param Client      $client
@param callable    $onMessage
@param callable    $write
@param string|null $settings HTTP2-Settings header content from upgrade request or null for direct HTTP/2.

@return \Generator
427347
public function setup(Client $client, callable $onMessage, callable $write, string $settings = null): \Generator
    {
        \assert(!$this->client, "The driver has already been setup");

        $this->client = $client;
        $this->onMessage = $onMessage;
        $this->write = $write;

        return $this->parser($settings);
    }
1353654
function setClient($clientName, MongoDB\Client $clientMongo)
    {
        if ($this->hasClient($clientName))
            throw new \Exception(sprintf('Client with name (%s) already exists and cant be replaced.', $clientName));


        $this->clients[$clientName] = $clientMongo;
        return $this;
    }
530999
public static function create(?Client $client = null, ?Configuration $config = null, ?EventManager $eventManager = null) : DocumentManager
    {
        return new static($client, $config, $eventManager);
    }
1154684
public function customSetting(string $settingKey, $settingValue): self
    {
        $this->clientConfig[$settingKey] = $settingValue;
        return $this;
    }
1205537
public static HttpClientContext newClientContext(HttpClientSettings settings,
                                                     Map<String, ? extends Object>
                                                             attributes) {
        final HttpClientContext clientContext = new HttpClientContext();

        if (attributes != null && !attributes.isEmpty()) {
            for (Map.Entry<String, ?> entry : attributes.entrySet()) {
                clientContext.setAttribute(entry.getKey(), entry.getValue());
            }
        }

        addPreemptiveAuthenticationProxy(clientContext, settings);

        clientContext.setAttribute(HttpContextUtils.DISABLE_SOCKET_PROXY_PROPERTY, settings.disableSocketProxy());
        return clientContext;

    }
1322818
public function generate(FileGenerator $file, $client): string
    {
        $class = $file->getClass() ?: new ClassGenerator();
        $class->setNamespaceName($client->getNamespace());
        $class->setName($client->getName());
        $methods = $client->getMethodMap();

        foreach ($methods->getMethods() as $method) {
            $this->ruleSet->applyRules(new ClientMethodContext($class, $method));
        }

        $file->setClass($class);

        return $file->generate();
    }
1315520
private function parser(string $settings = null): \Generator
    {
        $maxHeaderSize = $this->options->getHeaderSizeLimit();
        $maxBodySize = $this->options->getBodySizeLimit();
        $maxFramesPerSecond = $this->options->getFramesPerSecondLimit();
        $minAverageFrameSize = $this->options->getMinimumAverageFrameSize();

        $frameCount = 0;
        $bytesReceived = 0;
        $lastReset = $this->timeReference->getCurrentTime();

        if ($settings !== null) {
            if (\strlen($settings) % 6 !== 0) {
                $error = self::FRAME_SIZE_ERROR;
                goto connection_error;
            }

            while ($settings !== "") {
                if ($error = $this->updateSetting($settings)) {
                    goto connection_error;
                }
                $settings = \substr($settings, 6);
            }

            $this->clientWindow = $this->initialWindowSize;

            // Upgraded connections automatically assume an initial stream with ID 1.
            $this->streams[1] = new Http2Stream(
                0, // No data will be incoming on this stream.
                $this->initialWindowSize,
                Http2Stream::RESERVED | Http2Stream::REMOTE_CLOSED
            );
            $this->remainingStreams--;

            // Initial settings frame, sent immediately for upgraded connections.
            $this->writeFrame(
                \pack(
                    "nNnNnNnN",
                    self::INITIAL_WINDOW_SIZE,
                    $maxBodySize,
                    self::MAX_CONCURRENT_STREAMS,
                    $this->options->getConcurrentStreamLimit(),
                    self::MAX_HEADER_LIST_SIZE,
                    $maxHeaderSize,
                    self::MAX_FRAME_SIZE,
                    self::DEFAULT_MAX_FRAME_SIZE
                ),
                self::SETTINGS,
                self::NOFLAG
            );
        }

        $buffer = yield;

        while (\strlen($buffer) < \strlen(self::PREFACE)) {
            $buffer .= yield;
        }

        if (\strncmp($buffer, self::PREFACE, \strlen(self::PREFACE)) !== 0) {
            $error = self::PROTOCOL_ERROR;
            goto connection_error;
        }

        $buffer = \substr($buffer, \strlen(self::PREFACE));

        if ($this->client->isEncrypted() && ($this->client->getCryptoContext()["alpn_protocol"] ?? null) !== "h2") {
            $error = self::CONNECT_ERROR;
            goto connection_error;
        }

        if ($settings === null) {
            // Initial settings frame, delayed until after the preface is read for non-upgraded connections.
            $this->writeFrame(
                \pack(
                    "nNnNnNnN",
                    self::INITIAL_WINDOW_SIZE,
                    $maxBodySize,
                    self::MAX_CONCURRENT_STREAMS,
                    $this->options->getConcurrentStreamLimit(),
                    self::MAX_HEADER_LIST_SIZE,
                    $maxHeaderSize,
                    self::MAX_FRAME_SIZE,
                    self::DEFAULT_MAX_FRAME_SIZE
                ),
                self::SETTINGS,
                self::NOFLAG
            );
        }

        try {
            while (true) {
                if (++$frameCount === $maxFramesPerSecond) {
                    $now = $this->timeReference->getCurrentTime();
                    if ($lastReset === $now && $bytesReceived / $maxFramesPerSecond < $minAverageFrameSize) {
                        $error = self::ENHANCE_YOUR_CALM;
                        goto connection_error;
                    }

                    $lastReset = $now;
                    $frameCount = 0;
                    $bytesReceived = 0;
                }

                while (\strlen($buffer) < 9) {
                    $buffer .= yield;
                }

                $length = \unpack("N", "\0$buffer")[1];
                $bytesReceived += $length;

                if ($length > self::DEFAULT_MAX_FRAME_SIZE) { // Do we want to allow increasing max frame size?
                    $error = self::FRAME_SIZE_ERROR;
                    goto connection_error;
                }

                $type = $buffer[3];
                $flags = $buffer[4];
                $id = \unpack("N", \substr($buffer, 5, 4))[1];

                // the highest bit must be zero... but RFC does not specify what should happen when it is set to 1?
                /*if ($id < 0) {
                    $id = ~$id;
                }*/

                $buffer = \substr($buffer, 9);

                switch ($type) {
                    case self::DATA:
                        if ($id === 0) {
                            $error = self::PROTOCOL_ERROR;
                            goto connection_error;
                        }

                        if (!isset($this->streams[$id], $this->bodyEmitters[$id], $this->trailerDeferreds[$id])) {
                            // Technically it is a protocol error to send data to a never opened stream
                            // but we do not want to store what streams WE have closed via RST_STREAM,
                            // thus we're just reporting them as closed
                            $error = self::STREAM_CLOSED;
                            goto stream_error;
                        }

                        $stream = $this->streams[$id];

                        if ($stream->headers !== null) {
                            $error = self::PROTOCOL_ERROR;
                            goto stream_error;
                        }

                        if ($stream->state & Http2Stream::REMOTE_CLOSED) {
                            $error = self::PROTOCOL_ERROR;
                            goto stream_error;
                        }

                        if (($flags & self::PADDED) !== "\0") {
                            if ($buffer === "") {
                                $buffer = yield;
                            }
                            $padding = \ord($buffer);
                            $buffer = \substr($buffer, 1);
                            $length--;

                            if ($padding > $length) {
                                $error = self::PROTOCOL_ERROR;
                                goto connection_error;
                            }
                        } else {
                            $padding = 0;
                        }

                        $this->serverWindow -= $length;
                        $stream->serverWindow -= $length;
                        $stream->received += $length;

                        if ($stream->received >= $stream->maxBodySize && ($flags & self::END_STREAM) === "\0") {
                            $error = self::CANCEL;
                            goto stream_error;
                        }

                        if ($stream->serverWindow <= 0 && ($increment = $stream->maxBodySize - $stream->received)) {
                            if ($increment > self::MAX_INCREMENT) {
                                $increment = self::MAX_INCREMENT;
                            }

                            $stream->serverWindow += $increment;

                            $this->writeFrame(\pack("N", $increment), self::WINDOW_UPDATE, self::NOFLAG, $id);
                        }

                        if ($this->serverWindow <= 0) {
                            $increment = \max($stream->serverWindow, $maxBodySize);
                            $this->serverWindow += $increment;

                            $this->writeFrame(\pack("N", $increment), self::WINDOW_UPDATE, self::NOFLAG);
                        }

                        while (\strlen($buffer) < $length) {
                            /* it is fine to just .= the $body as $length < 2^14 */
                            $buffer .= yield;
                        }

                        $body = \substr($buffer, 0, $length - $padding);
                        $buffer = \substr($buffer, $length);
                        if ($body !== "") {
                            $buffer .= yield $this->bodyEmitters[$id]->emit($body);
                        }

                        if (($flags & self::END_STREAM) !== "\0") {
                            $stream->state |= Http2Stream::REMOTE_CLOSED;

                            if (!isset($this->bodyEmitters[$id], $this->trailerDeferreds[$id])) {
                                continue 2; // Stream closed after emitting body fragment.
                            }

                            $deferred = $this->trailerDeferreds[$id];
                            $emitter = $this->bodyEmitters[$id];

                            unset($this->bodyEmitters[$id], $this->trailerDeferreds[$id]);

                            $deferred->resolve(new Trailers([]));
                            $emitter->complete();
                        }

                        continue 2;

                    case self::HEADERS:
                        if (isset($this->streams[$id])) {
                            $stream = $this->streams[$id];

                            if ($stream->headers !== null) {
                                $error = self::PROTOCOL_ERROR;
                                goto connection_error;
                            }

                            if ($stream->state & Http2Stream::REMOTE_CLOSED) {
                                $error = self::STREAM_CLOSED;
                                goto stream_error;
                            }
                        } else {
                            if ($this->remainingStreams-- <= 0) {
                                $error = self::PROTOCOL_ERROR;
                                goto connection_error;
                            }

                            $stream = $this->streams[$id] = new Http2Stream($maxBodySize, $this->initialWindowSize);
                        }

                        if (($flags & self::PADDED) !== "\0") {
                            if ($buffer === "") {
                                $buffer = yield;
                            }
                            $padding = \ord($buffer);
                            $buffer = \substr($buffer, 1);
                            $length--;
                        } else {
                            $padding = 0;
                        }

                        if (($flags & self::PRIORITY_FLAG) !== "\0") {
                            while (\strlen($buffer) < 5) {
                                $buffer .= yield;
                            }

                            /* Not needed until priority is handled.
                            $dependency = unpack("N", $buffer)[1];
                            if ($dependency < 0) {
                                $dependency = ~$dependency;
                                $exclusive = true;
                            } else {
                                $exclusive = false;
                            }

                            $weight = $buffer[4];
                            */

                            $buffer = \substr($buffer, 5);
                            $length -= 5;
                        }

                        if ($padding >= $length) {
                            $error = self::PROTOCOL_ERROR;
                            goto connection_error;
                        }

                        if ($length > $maxHeaderSize) {
                            $error = self::ENHANCE_YOUR_CALM;
                            goto stream_error;
                        }

                        while (\strlen($buffer) < $length) {
                            $buffer .= yield;
                        }

                        $stream->headers = \substr($buffer, 0, $length - $padding);
                        $buffer = \substr($buffer, $length);

                        if (($flags & self::END_STREAM) !== "\0") {
                            $stream->state |= Http2Stream::REMOTE_CLOSED;
                        }

                        if (($flags & self::END_HEADERS) !== "\0") {
                            goto parse_headers;
                        }

                        continue 2;

                    case self::PRIORITY:
                        if ($length != 5) {
                            $error = self::FRAME_SIZE_ERROR;
                            goto connection_error;
                        }

                        if ($id === 0) {
                            $error = self::PROTOCOL_ERROR;
                            goto connection_error;
                        }

                        while (\strlen($buffer) < 5) {
                            $buffer .= yield;
                        }

                        /* @TODO PRIORITY frames values not used.
                        $dependency = unpack("N", $buffer);

                        if ($dependency < 0) {
                            $dependency = ~$dependency;
                            $exclusive = true;
                        } else {
                            $exclusive = false;
                        }

                        if ($dependency == 0) {
                            $error = self::PROTOCOL_ERROR;
                            goto connection_error;
                        }

                        $weight = $buffer[4];
                        */

                        $buffer = \substr($buffer, 5);
                        continue 2;

                    case self::RST_STREAM:
                        if ($length !== 4) {
                            $error = self::FRAME_SIZE_ERROR;
                            goto connection_error;
                        }

                        if ($id === 0) {
                            $error = self::PROTOCOL_ERROR;
                            goto connection_error;
                        }

                        while (\strlen($buffer) < 4) {
                            $buffer .= yield;
                        }

                        $error = \unpack("N", $buffer)[1];

                        if (isset($this->bodyEmitters[$id], $this->trailerDeferreds[$id])) {
                            $exception = new ClientException("Client ended stream", self::STREAM_CLOSED);

                            $emitter = $this->bodyEmitters[$id];
                            $deferred = $this->trailerDeferreds[$id];

                            unset($this->bodyEmitters[$id], $this->trailerDeferreds[$id]);

                            $emitter->fail($exception);
                            $deferred->fail($exception);
                        }

                        if (isset($this->streams[$id])) {
                            $this->releaseStream($id);
                        }

                        $buffer = \substr($buffer, 4);
                        continue 2;

                    case self::SETTINGS:
                        if ($id !== 0) {
                            $error = self::PROTOCOL_ERROR;
                            goto connection_error;
                        }

                        if (($flags & self::ACK) !== "\0") {
                            if ($length) {
                                $error = self::PROTOCOL_ERROR;
                                goto connection_error;
                            }

                            // Got ACK
                            continue 2;
                        }

                        if ($length % 6 !== 0) {
                            $error = self::FRAME_SIZE_ERROR;
                            goto connection_error;
                        }

                        if ($length > 60) {
                            // Even with room for a few future options, sending that a big SETTINGS frame is just about
                            // wasting our processing time. I hereby declare this a protocol error.
                            $error = self::PROTOCOL_ERROR;
                            goto connection_error;
                        }

                        while ($length > 0) {
                            while (\strlen($buffer) < 6) {
                                $buffer .= yield;
                            }

                            if ($error = $this->updateSetting($buffer)) {
                                goto connection_error;
                            }

                            $buffer = \substr($buffer, 6);
                            $length -= 6;
                        }

                        $this->writeFrame("", self::SETTINGS, self::ACK);
                        continue 2;

                    // PUSH_PROMISE sent by client is a PROTOCOL_ERROR (just like undefined frame types)

                    case self::PING:
                        if ($length !== 8) {
                            $error = self::FRAME_SIZE_ERROR;
                            goto connection_error;
                        }

                        if ($id !== 0) {
                            $error = self::PROTOCOL_ERROR;
                            goto connection_error;
                        }

                        while (\strlen($buffer) < 8) {
                            $buffer .= yield;
                        }

                        $data = \substr($buffer, 0, 8);

                        if (($flags & self::ACK) === "\0") {
                            $this->writeFrame($data, self::PING, self::ACK);
                        }

                        $buffer = \substr($buffer, 8);

                        continue 2;

                    case self::GOAWAY:
                        if ($id !== 0) {
                            $error = self::PROTOCOL_ERROR;
                            goto connection_error;
                        }

                        $lastId = \unpack("N", $buffer)[1];
                        // the highest bit must be zero... but RFC does not specify what should happen when it is set to 1?
                        if ($lastId < 0) {
                            $lastId = ~$lastId;
                        }
                        $error = \unpack("N", \substr($buffer, 4, 4))[1];

                        $buffer = \substr($buffer, 8);
                        $length -= 8;

                        while (\strlen($buffer) < $length) {
                            $buffer .= yield;
                        }

                        if ($error !== 0) {
                            // @TODO Log error, since the client says we made a boo-boo.
                        }

                        yield $this->shutdown($lastId);
                        $this->client->close();

                        return;

                    case self::WINDOW_UPDATE:
                        if ($length !== 4) {
                            $error = self::FRAME_SIZE_ERROR;
                            goto connection_error;
                        }

                        while (\strlen($buffer) < 4) {
                            $buffer .= yield;
                        }

                        if ($buffer === "\0\0\0\0") {
                            $error = self::PROTOCOL_ERROR;
                            if ($id) {
                                goto stream_error;
                            }
                            goto connection_error;
                        }

                        $windowSize = \unpack("N", $buffer)[1];
                        $buffer = \substr($buffer, 4);

                        if ($id) {
                            // May receive a WINDOW_UPDATE frame for a closed stream.
                            if (isset($this->streams[$id])) {
                                $this->streams[$id]->clientWindow += $windowSize;

                                if ($this->streams[$id]->buffer !== "") {
                                    $this->writeBufferedData($id);
                                }
                            }

                            continue 2;
                        }

                        $this->clientWindow += $windowSize;

                        foreach ($this->streams as $id => $stream) {
                            if ($stream->buffer === "") {
                                continue;
                            }

                            $this->writeBufferedData($id);

                            if ($this->clientWindow === 0) {
                                break;
                            }
                        }

                        continue 2;

                    case self::CONTINUATION:
                        if (!isset($this->streams[$id])) {
                            // technically it is a protocol error to send data to a never opened stream
                            // but we do not want to store what streams WE have closed via RST_STREAM,
                            // thus we're just reporting them as closed
                            $error = self::STREAM_CLOSED;
                            goto stream_error;
                        }

                        $stream = $this->streams[$id];

                        if ($stream->headers === null) {
                            $error = self::PROTOCOL_ERROR;
                            goto stream_error;
                        }

                        if ($stream->state & Http2Stream::REMOTE_CLOSED) {
                            $error = self::STREAM_CLOSED;
                            goto stream_error;
                        }

                        if ($length > $maxHeaderSize - \strlen($stream->headers)) {
                            $error = self::ENHANCE_YOUR_CALM;
                            goto stream_error;
                        }

                        while (\strlen($buffer) < $length) {
                            $buffer .= yield;
                        }

                        $stream->headers .= \substr($buffer, 0, $length);
                        $buffer = \substr($buffer, $length);

                        if (($flags & self::END_STREAM) !== "\0") {
                            $stream->state |= Http2Stream::REMOTE_CLOSED;
                        }

                        if (($flags & self::END_HEADERS) !== "\0") {
                            goto parse_headers;
                        }

                        continue 2;

                    default:
                        $error = self::PROTOCOL_ERROR;
                        goto connection_error;
                }

                parse_headers: {
                    $decoded = $this->table->decode($stream->headers, $maxHeaderSize);
                    $stream->headers = null;

                    if ($decoded === null) {
                        $error = self::COMPRESSION_ERROR;
                        goto stream_error;
                    }

                    $headers = [];
                    foreach ($decoded as list($name, $value)) {
                        if (!\preg_match(self::HEADER_NAME_REGEX, $name)) {
                            $error = self::PROTOCOL_ERROR;
                            goto stream_error;
                        }

                        $headers[$name][] = $value;
                    }

                    if (isset($this->trailerDeferreds[$id]) && $stream->state & Http2Stream::RESERVED) {
                        $deferred = $this->trailerDeferreds[$id];
                        $emitter = $this->bodyEmitters[$id];

                        unset($this->bodyEmitters[$id], $this->trailerDeferreds[$id]);

                        // Trailers must not contain pseudo-headers.
                        foreach ($headers as $name => $value) {
                            if ($name[0] === ':') {
                                $error = self::PROTOCOL_ERROR;
                                goto stream_error;
                            }
                        }

                        $deferred->resolve(new Trailers($headers));
                        $emitter->complete();

                        continue;
                    }

                    if ($stream->state & Http2Stream::RESERVED) {
                        $error = self::PROTOCOL_ERROR;
                        goto stream_error;
                    }

                    $stream->state |= Http2Stream::RESERVED;

                    if ($this->stopping) {
                        continue; // Do not dispatch more requests if stopping.
                    }

                    if (!isset($headers[":method"][0])) {
                        $error = self::PROTOCOL_ERROR;
                        goto stream_error;
                    }

                    $target = $headers[":path"][0] ?? "";
                    $scheme = $headers[":scheme"][0] ?? ($this->client->isEncrypted() ? "https" : "http");
                    $host = $headers[":authority"][0] ?? "";
                    $query = null;

                    if (!\preg_match("#^([A-Z\d\.\-]+|\[[\d:]+\])(?::([1-9]\d*))?$#i", $host, $matches)) {
                        $error = self::PROTOCOL_ERROR;
                        goto stream_error;
                    }

                    $host = $matches[1];
                    $port = isset($matches[2]) ? (int) $matches[2] : $this->client->getLocalPort();

                    if ($position = \strpos($target, "#")) {
                        $target = \substr($target, 0, $position);
                    }

                    if ($position = \strpos($target, "?")) {
                        $query = \substr($target, $position + 1);
                        $target = \substr($target, 0, $position);
                    }

                    try {
                        $uri = Uri\Http::createFromComponents([
                            "scheme" => $scheme,
                            "host"   => $host,
                            "port"   => $port,
                            "path"   => $target,
                            "query"  => $query,
                        ]);
                    } catch (Uri\UriException $exception) {
                        $error = self::PROTOCOL_ERROR;
                        goto stream_error;
                    }

                    if ($stream->state & Http2Stream::REMOTE_CLOSED) {
                        $request = new Request(
                            $this->client,
                            $headers[":method"][0],
                            $uri,
                            $headers,
                            null,
                            "2.0"
                        );

                        $this->streamIdMap[\spl_object_hash($request)] = $id;
                        $stream->pendingResponse = ($this->onMessage)($request);

                        // Must null reference to Request object so it is destroyed when request handler completes.
                        $request = null;

                        continue;
                    }

                    $this->trailerDeferreds[$id] = $deferred = new Deferred;
                    $this->bodyEmitters[$id] = new Emitter;

                    $body = new RequestBody(
                        new IteratorStream($this->bodyEmitters[$id]->iterate()),
                        function (int $bodySize) use ($id) {
                            if (!isset($this->streams[$id], $this->bodyEmitters[$id])) {
                                return;
                            }

                            if ($this->streams[$id]->maxBodySize >= $bodySize) {
                                return;
                            }

                            $this->streams[$id]->maxBodySize = $bodySize;
                        },
                        $deferred->promise()
                    );

                    if ($this->serverWindow <= $maxBodySize >> 1) {
                        $increment = $maxBodySize - $this->serverWindow;
                        $this->serverWindow = $maxBodySize;
                        $this->writeFrame(\pack("N", $increment), self::WINDOW_UPDATE, self::NOFLAG);
                    }

                    $request = new Request(
                        $this->client,
                        $headers[":method"][0],
                        $uri,
                        $headers,
                        $body,
                        "2.0"
                    );

                    $this->streamIdMap[\spl_object_hash($request)] = $id;
                    $stream->pendingResponse = ($this->onMessage)($request);

                    // Must null reference to Request and Body objects
                    // so they are destroyed when request handler completes.
                    $request = $body = null;

                    continue;
                }

                stream_error: {
                    $this->writeFrame(\pack("N", $error), self::RST_STREAM, self::NOFLAG, $id);

                    if (isset($this->bodyEmitters[$id], $this->trailerDeferreds[$id])) {
                        $exception = new ClientException("Stream error", self::CANCEL);

                        $this->trailerDeferreds[$id]->fail($exception);
                        $this->bodyEmitters[$id]->fail($exception);
                    }

                    if (isset($this->streams[$id])) {
                        $this->releaseStream($id);
                    }

                    // consume whole frame to be able to continue this connection
                    $length -= \strlen($buffer);
                    while ($length > 0) {
                        $buffer = yield;
                        $length -= \strlen($buffer);
                    }
                    $buffer = \substr($buffer, \strlen($buffer) + $length);

                    continue;
                }
            }
        } finally {
            if (!empty($this->bodyEmitters) || !empty($this->trailerDeferreds)) {
                $exception = new ClientException("Client disconnected", self::CANCEL);

                foreach ($this->trailerDeferreds as $id => $deferred) {
                    unset($this->trailerDeferreds[$id]);
                    $deferred->fail($exception);
                }

                foreach ($this->bodyEmitters as $id => $emitter) {
                    unset($this->bodyEmitters[$id]);
                    $emitter->fail($exception);
                }
            }
        }

        connection_error: {
            yield $this->shutdown(null, $error ?? self::PROTOCOL_ERROR);
            $this->client->close();
        }
    }
468666
public function generateMessage(EmailInterface $email, $to, array $parameters = array(), $locale = null)
    {
        if (null === $locale) {
            $locale = $this->options['default_locale'];
        }

        if (is_object($to)) {
            $to = $this->getRecipient($to);
        }

        try {
            $email->setLocale($locale);
            $this->renderer->loadTemplates($email);

            $message = $this->createMessageInstance()
                            ->setSubject($this->renderTemplate('subject', $parameters, $email->getChecksum()))
                            ->setFrom($this->renderFromAddress($email, $parameters), $this->renderTemplate('from_name', $parameters, $email->getChecksum()))
                            ->setTo($to)
                            ->setBody($this->renderTemplate('html_content', $parameters, $email->getChecksum()), 'text/html');

            $textContent = $this->renderTemplate('text_content', $parameters, $email->getChecksum());

            if (null !== $textContent && '' !== $textContent) {
                $message->addPart($textContent, 'text/plain');
            }

            foreach ($email->getBccs() as $bcc) {
                $message->addBcc($bcc);
            }

            if (count($email->getHeaders()) > 0) {
                $headers = $message->getHeaders();
                foreach ($email->getHeaders() as $header) {
                    if (is_array($header) && isset($header['key'], $header['value'])) {
                        $headers->addTextHeader($header['key'], $header['value']);
                    }
                }
            }

        } catch (NoTranslationException $e) {
            $message = new NoTranslationMessage($email->getReference(), $locale);
            $message->setFrom($this->options['admin_email']);
            $message->setTo($this->options['admin_email']);

        } catch (\Twig_Error_Runtime $e) {
            $message = new UndefinedVariableMessage($e->getMessage(), $email->getReference());
            $message->setFrom($this->options['admin_email']);
            $message->setTo($this->options['admin_email']);

        } catch (\Twig_Error $e) {
            $message = new TwigErrorMessage($e->getRawMessage(), $email->getReference());
            $message->setFrom($this->options['admin_email']);
            $message->setTo($this->options['admin_email']);
        }

        return $message;
    }
205174
public function settings()
    {
        if (is_null($this->settingRequests)) {
            $this->settingRequests = new SettingRequests($this->client);
        }

        return $this->settingRequests;
    }
1049906
public function create(ClientInterface $client): ClientInterface
    {
        try {
            $now    = $this->ignoreException(function (): DateTimeImmutable {
                return new DateTimeImmutable();
            });
            $schema = $this->getDatabaseSchema();
            $values = [
                $schema->getClientsIdentityColumn()               => $client->getIdentifier(),
                $schema->getClientsNameColumn()                   => $client->getName(),
                $schema->getClientsDescriptionColumn()            => $client->getDescription(),
                $schema->getClientsCredentialsColumn()            => $client->getCredentials(),
                $schema->getClientsIsConfidentialColumn()         => $client->isConfidential(),
                $schema->getClientsIsScopeExcessAllowedColumn()   => $client->isScopeExcessAllowed(),
                $schema->getClientsIsUseDefaultScopeColumn()      => $client->isUseDefaultScopesOnEmptyRequest(),
                $schema->getClientsIsCodeGrantEnabledColumn()     => $client->isCodeGrantEnabled(),
                $schema->getClientsIsImplicitGrantEnabledColumn() => $client->isImplicitGrantEnabled(),
                $schema->getClientsIsPasswordGrantEnabledColumn() => $client->isPasswordGrantEnabled(),
                $schema->getClientsIsClientGrantEnabledColumn()   => $client->isClientGrantEnabled(),
                $schema->getClientsIsRefreshGrantEnabledColumn()  => $client->isRefreshGrantEnabled(),
                $schema->getClientsCreatedAtColumn()              => $now,
            ];

            $identifier = $client->getIdentifier();
            if (empty($scopeIdentifiers = $client->getScopeIdentifiers()) === true) {
                $this->createResource($values);
            } else {
                $this->inTransaction(function () use ($identifier, $values, $scopeIdentifiers) {
                    $this->createResource($values);
                    $this->bindScopeIdentifiers($identifier, $scopeIdentifiers);
                });
            }
            $client->setCreatedAt($now);

            return $client;
        } catch (RepositoryException $exception) {
            $message = 'Client creation failed.';
            throw new RepositoryException($message, 0, $exception);
        }
    }
1135831
protected function createResourceFromClient(ClientEntity $client)
    {
        $resource = new Resource(
            $this->generateReadUrl($client->getId()),
            array(
                 'id'           => $client->getId(),
                 'publicId'     => $client->getPublicId(),
                 'secret'       => $client->getSecret(),
                 'redirectUris' => $client->getRedirectUris(),
                 'grants'       => $client->getAllowedGrantTypes(),
            )
        );

        return $resource;
    }
65056
protected function createRequest($client, $method, $uri = '', array $parameters = [])
    {
        /**
         * @see \JonnyW\PhantomJs\Http\Request
         **/
        $request = $client->getMessageFactory()->createRequest($uri, $method);
        $request->addHeader(
            'User-Agent',
            'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:57.0) Gecko/20100101 Firefox/57.0'
        );

        $request->addHeader(
            'Content-Type',
            'application/x-www-form-urlencoded'
        );

        if (isset($parameters['form_params'])) {
            $request->setRequestData($parameters['form_params']);
        }

        return $request;
    }
591216
public function setClient(Client $client)
    {
        // Assign the client.
        $this->client = $client;
        $this->client->setDefaultOption('headers', ['Content-Type' => 'application/json']);

        // Allow chaining.
        return $this;
    }
26126
public static function configure($settings)
    {
        if (isset($settings['client_id'])) {
            self::configureOAuth($settings);
        } else {
            self::configureBasicAuth($settings);
        }
    }
699061
public static function createFromClient(Client $client, $includeTerminalType = TRUE)
	{
		$request = new Request();

		$request->setInterface($client->getInterface());

		if ($includeTerminalType) {
			$request->setTerminalType($client->getTerminalType());
		}

		return $request;
	}
651113
public static function setKey($key, ClientInterface $client = null)
	{
		self::$api = new Api($key, $client);

		return self::$api;
	}
376574
protected function setToken(Client $client)
    {
        $token = getenv('BOWERPHP_TOKEN');
        if (!empty($token)) {
            $client->authenticate($token, null, Client::AUTH_HTTP_TOKEN);
        }
    }