public static function sign(RequestInterface $request, Credentials $credentials)
    {
        /**
         * @var RequestInterface $request
         */

        $signatureHeaders   = [];
        $base64Md5          = '';
        $contentLength      = '';

        if ($request->getBody()->getSize() !== 0) {
            $base64Md5 = Hmac::md5HashBase64(
                $request->getBody()
                    ->getContents()
            );

            $contentLength = strlen((string)$request->getBody());
            $signatureHeaders = array_merge(
                $signatureHeaders,
                [
                    'content-type'      => $request->getHeader('content-type'),
                    'content-md5'       => $base64Md5,
                    'content-length'    => $contentLength
                ]);
        }

        $parsedUrl = parse_url($request->getUri());
        $targetUrl = $parsedUrl["path"];
        if (!empty($parsedUrl["query"])) {
            $targetUrl = $targetUrl . "?" . $parsedUrl["query"];
        }

        $requestLine = $request->getMethod() . " " . $targetUrl . " HTTP/1.1";

        $signatureHeaders['date'] = $date = gmdate("D, d M Y H:i:s", time()) . " GMT";
        $signatureHeaders['request-line'] = $requestLine;

        $headersString      = Hmac::getHeadersString($signatureHeaders);
        $signatureString    = Hmac::getSignatureString($signatureHeaders);
        $signatureHash      = Hmac::shaHashBase64($signatureString, $credentials->getSecret());
        $authHeader         = sprintf(
            'hmac username="%s", algorithm="hmac-%s", headers="%s", signature="%s"',
            $credentials->getAccessKey(),
            Hmac::ALGORITHM_SHA256,
            $headersString,
            $signatureHash
        );

        $request = $request->withHeader('Authorization', $authHeader)
            ->withHeader('Date', $date);

        if ($request->getBody()->getSize()) {
            return $request->withHeader('Content-Type', $request->getHeader('content-type'))
                ->withHeader('Content-MD5', $base64Md5)
                ->withHeader('Content-Length', $contentLength);
        }

        return $request;
    }