You are here

public function Debugger::extractResponseInfo in Acquia Purge 8

Extract information from a response.

Parameters

\Psr\Http\Message\ResponseInterface $response: The HTTP response object.

bool $body_title: Whether the respone body should be a titled array key.

Return value

string[] Tabular information which could be fed to ::writeTable().

Overrides DebuggerInterface::extractResponseInfo

1 call to Debugger::extractResponseInfo()
Debugger::logFailedRequest in src/Plugin/Purge/Purger/Debugger.php
Log the given failure with as much info as possible.

File

src/Plugin/Purge/Purger/Debugger.php, line 131

Class

Debugger
Provides a centralized debugger for Acquia purger plugins.

Namespace

Drupal\acquia_purge\Plugin\Purge\Purger

Code

public function extractResponseInfo(ResponseInterface $response, $body_title = FALSE) {
  $info = [];
  $info['RSP'] = sprintf("HTTP/%s %d %s", $response
    ->getProtocolVersion(), $response
    ->getStatusCode(), $response
    ->getReasonPhrase());
  foreach ($response
    ->getHeaders() as $header => $value) {
    $info['RSP ' . $header] = implode(',', $value);
  }
  if ($body = (string) $response
    ->getBody()) {
    $content_type = $response
      ->getHeaderLine('content-type');
    if (strpos($content_type, 'application/json') !== FALSE) {
      $body = json_decode($body);
      $body = json_encode($body, JSON_PRETTY_PRINT);
    }
    elseif (strpos($content_type, 'text/html') !== FALSE) {
      if (count($lines = explode("\n", $body)) >= 4) {
        $lines = array_slice($lines, 0, 4);
        $body = implode("\n", $lines) . "\n...";
      }
    }
    if ($body_title) {
      $info['RSP BODY'] = $body;
    }
    else {
      $info[] = '';
      $info[] = $body;
    }
  }
  return $info;
}