protected function injectProfiler(ResponseInterface $response, string $token): ResponseInterface
    {
        $content         = (string) $response->getBody();
        $renderedContent = $this->createTemplate($token);

        $pos = \mb_strripos($content, '</body>');

        if ($pos !== false) {
            $stream = $this->streamFactory->createStream(
                \mb_substr($content, 0, $pos) . $renderedContent . \mb_substr($content, $pos)
            );

            // Update the new content and reset the content length
            $response = $response->withoutHeader('content-length');

            return $response->withBody($stream);
        }

        $response->getBody()->write($renderedContent);

        return $response;
    }