You are here

public function ApiClientProfiler::__invoke in Apigee Edge 8

File

modules/apigee_edge_debug/src/HttpClientMiddleware/ApiClientProfiler.php, line 84

Class

ApiClientProfiler
Http client middleware that profiles Apigee Edge API calls.

Namespace

Drupal\apigee_edge_debug\HttpClientMiddleware

Code

public function __invoke() {

  // If the formatter has been initialized yet then do nothing.
  if (!$this->formatter) {
    return function ($handler) {
      return function (RequestInterface $request, array $options) use ($handler) {
        return $handler($request, $options);
      };
    };
  }
  return function ($handler) {
    return function (RequestInterface $request, array $options) use ($handler) {

      // If this request already has an on_stats callback do not override
      // it. Store it and call it after ours.
      if (isset($options[RequestOptions::ON_STATS])) {
        $next = $options[RequestOptions::ON_STATS];
      }
      else {
        $next = function (TransferStats $stats) {
        };
      }
      $logger = $this->logger;
      $formatter = $this->formatter;
      $log_format = $this->logFormat;
      $options[RequestOptions::ON_STATS] = function (TransferStats $stats) use ($request, $next, $logger, $formatter, $log_format) {

        // Do not modify the original request object in the subsequent calls.
        $request_clone = clone $request;
        $level = LogLevel::DEBUG;

        // Do not log this request if it has not been made by the Apigee Edge
        // SDK connector.
        if (!$request_clone
          ->hasHeader(SDKConnector::HEADER)) {
          return;
        }
        $debugMessageTitle = explode("\n", trim($formatter
          ->formatRequest($request_clone)));
        $debugMessageTitle = str_replace('/v1/organizations/***organization***', '', $debugMessageTitle[0]);
        $context = [
          'request_formatted' => $debugMessageTitle . "\n>>>>>>>>\n" . $formatter
            ->formatRequest($request_clone),
          'stats' => $formatter
            ->formatStats($stats),
        ];
        if ($stats
          ->hasResponse()) {

          // Do not modify the original response object in the subsequent
          // calls.
          $response_clone = clone $stats
            ->getResponse();
          $context['response_formatted'] = "<<<<<<<<\n" . $formatter
            ->formatResponse($response_clone, $request_clone);
          if ($stats
            ->getResponse()
            ->getStatusCode() >= 400) {
            $level = LogLevel::WARNING;
          }
        }
        else {
          $level = LogLevel::ERROR;
          $error = $stats
            ->getHandlerErrorData();
          if (is_object($error)) {
            if (method_exists($error, '__toString')) {
              $error = (string) $error;
            }
            else {
              $error = json_encode($error);
            }
          }
          $context['error'] = $error;
        }
        $logger
          ->log($level, $log_format, $context);
        $next($stats);
      };
      return $handler($request, $options);
    };
  };
}