Convert a PSR-7 response to ZF2 response

@param  ResponseInterface $response
@return HttpResponse
320679
private function convertToZfResponse(ResponseInterface $response)
    {
        $zfResponse = new HttpResponse();

        $zfResponse->setStatusCode($response->getStatusCode());
        $zfResponse->setReasonPhrase($response->getReasonPhrase());
        $zfResponse->setContent((string) $response->getBody());

        foreach ($response->getHeaders() as $name => $values) {
            $zfResponse->getHeaders()->addHeaderLine($name, implode(", ", $values));
        }

        return $zfResponse;
    }
631164
private function decodeResponse( ResponseInterface $response ) {
		$resultArray = json_decode( $response->getBody(), true );

		$this->logWarnings( $resultArray );
		$this->throwUsageExceptions( $resultArray );

		return $resultArray;
	}
34892
private static function range(ResponseInterface $response, string $range): ResponseInterface
    {
        $response = $response->withHeader('Accept-Ranges', 'bytes');

        if (empty($range) || !($range = self::parseRangeHeader($range))) {
            return $response;
        }

        list($first, $last) = $range;
        $size = $response->getBody()->getSize();

        if ($last === null) {
            $last = $size - 1;
        }

        return $response
            ->withStatus(206)
            ->withHeader('Content-Length', (string) ($last - $first + 1))
            ->withHeader('Content-Range', sprintf('bytes %d-%d/%d', $first, $last, $size));
    }
871800
protected function _unserializeResponseBody(ResponseInterface $response) {

		$body = (string) $response->getBody();
		$contentType = $response->getHeaderLine('Content-type');

		try {
			if(false !== stripos($contentType, $this->dataType)
				&& isset($this->unserializers[$this->dataType])) {
				/** @var UnserializerInterface $unserializer */
				$unserializer = \Yii::createObject($this->unserializers[$this->dataType]);
				if($unserializer instanceof UnserializerInterface) {
					return $unserializer->unserialize($body, false);
				}
			}

			return $body;

		} catch(InvalidParamException $e) {

			return $body;
		}
	}
175219
public function prepareResponse($response)
    {
        $request = \app(Request::class);

        if ($response instanceof Responsable) {
            $response = $response->toResponse($request);
        }

        if ($response instanceof PsrResponseInterface) {
            $response = (new HttpFoundationFactory())->createResponse($response);
        } elseif (! $response instanceof SymfonyResponse) {
            $response = new Response($response);
        } elseif ($response instanceof BinaryFileResponse) {
            $response = $response->prepare(Request::capture());
        }

        return $response->prepare($request);
    }
28334
protected function parseTokenResponse(ResponseInterface $response):AccessToken{
		$data = Psr7\get_json($response, true);

		if(!$data || !is_array($data)){
			throw new ProviderException('unable to parse token response');
		}
		elseif(isset($data['error'])){
			throw new ProviderException('error retrieving access token: '.$data['message']);
		}
		elseif(!isset($data['session']['key'])){
			throw new ProviderException('token missing');
		}

		$token = new AccessToken([
			'provider'    => $this->serviceName,
			'accessToken' => $data['session']['key'],
			'expires'     => AccessToken::EOL_NEVER_EXPIRES,
		]);

		unset($data['session']['key']);

		$token->extraParams = $data;

		$this->storage->storeAccessToken($this->serviceName, $token);

		return $token;
	}
1296089
protected function generateResponse(ResponseInterface $response): DepartureBoardResponse
    {
        $json = $this->validateJson($response);
        if (isset($json['DepartureBoard'])) {
            return DepartureBoardResponse::createFromArray($json['DepartureBoard']);
        }

        return new DepartureBoardResponse();
    }
549955
protected function responseWithHeaders($response)
    {
        foreach ($this->getHeaders() as $key => $value) {
            $response->headers->set($key, $value);
        }

        return $response;
    }
851964
public function convert(ResponseInterface $psrResponse)
    {
        return new BrowserKitResponse(
            (string)$psrResponse->getBody(),
            $psrResponse->getStatusCode() ?: 200,
            $this->flattenHeaders($psrResponse->getHeaders())
        );
    }
237233
private function fromPsrResponse(ResponseInterface $response)
    {
        return new Response(
            $response->getStatusCode(),
            $response->getHeaders(),
            $response->getBody(),
            $response->getProtocolVersion(),
            $response->getReasonPhrase()
        );
    }
313299
public function sendHeaders(ResponseInterface $response)
    {
        foreach ($response->getHeaders() as $header => $values) {
            $first = true;
            $header = HeaderHelper::normalizeHeaderName($header);

            foreach ($values as $value) {
                $this->header(sprintf('%s: %s', $header, $value), $first);

                $first = false;
            }
        }

        return $this;
    }
631118
public function fromResponse(HttpResponseInterface $response)
    {
        return $this->createResponse(
            $response->getStatusCode(),
            $response->getHeaders(),
            JsonRpc\json_decode((string) $response->getBody(), true) ?: []
        );
    }
1367363
protected function render(ResponseInterface $response)
    {
        $stream = new Stream('php://memory', 'rw+');
        $content = $this->getEngine()
            ->parse($this->getTemplate())
            ->process($this->getData())
        ;
        $stream->write($content);

        $response = $response->withBody($stream);
        return $response;
    }
93979
public function convertResponse($response, $serviceResource)
    {
        $resourceName = substr($serviceResource, 0, -2);
        return $this->_convertResponseToModel($response, $resourceName);
    }
1045214
public function getUrlOfResponse( ResponseInterface $response ) {
		$ref = InternalServer::storeResponse($this->tmpDir, $response);

		return $this->getServerRoot() . '/' . self::VND . '/' . $ref;
	}
616333
private function attachNocacheCacheLimiterHeader(ResponseInterface $response): ResponseInterface
    {
        return $response
            ->withAddedHeader('Expires', self::EXPIRED)
            ->withAddedHeader('Cache-Control', 'no-store, no-cache, must-revalidate')
            ->withAddedHeader('Pragma', 'no-cache');
    }
717661
private function processResponse(ResponseInterface $response)
    {
        if (200 !== $response->getStatusCode()) {
            throw new \RuntimeException('Query to the API server did not result in an appropriate response code');
        }

        $body = guzzle_json_decode((string) $response->getBody(), true);

        if ('OK' !== $body['error']) {
            throw new \RuntimeException('Query to the API server did not result in an appropriate response code');
        }

        return $body;
    }