You are here

final class DevelKintApiClientProfiler in Apigee Edge 8

Http client middleware that profiles Apigee Edge API calls.

Hierarchy

Expanded class hierarchy of DevelKintApiClientProfiler

1 string reference to 'DevelKintApiClientProfiler'
apigee_edge_debug.services.yml in modules/apigee_edge_debug/apigee_edge_debug.services.yml
modules/apigee_edge_debug/apigee_edge_debug.services.yml
1 service uses DevelKintApiClientProfiler
apigee_edge_debug.devel_client_profiler in modules/apigee_edge_debug/apigee_edge_debug.services.yml
Drupal\apigee_edge_debug\HttpClientMiddleware\DevelKintApiClientProfiler

File

modules/apigee_edge_debug/src/HttpClientMiddleware/DevelKintApiClientProfiler.php, line 37

Namespace

Drupal\apigee_edge_debug\HttpClientMiddleware
View source
final class DevelKintApiClientProfiler {

  /**
   * The currently logged-in user.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  private $currentUser;

  /**
   * The messenger service.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface
   */
  private $messenger;

  /**
   * The debug message formatter plugin.
   *
   * @var \Drupal\apigee_edge_debug\Plugin\DebugMessageFormatter\DebugMessageFormatterPluginInterface
   */
  private $formatter;

  /**
   * The module handler.
   *
   * @var \Drupal\Core\Extension\ModuleHandlerInterface|null
   */
  private $moduleHandler;

  /**
   * DevelKintApiClientProfiler constructor.
   *
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   Config factory.
   * @param \Drupal\apigee_edge_debug\DebugMessageFormatterPluginManager $debug_message_formatter_plugin
   *   Debug message formatter plugin manager.
   * @param \Drupal\Core\Session\AccountInterface $currentUser
   *   The currently logged-in user.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $module_handler
   *   The module handler service.
   * @param \Drupal\Core\Messenger\MessengerInterface $messenger
   *   The messenger service.
   */
  public function __construct(ConfigFactoryInterface $config_factory, DebugMessageFormatterPluginManager $debug_message_formatter_plugin, AccountInterface $currentUser, ModuleHandlerInterface $module_handler, MessengerInterface $messenger) {

    // On module install, this constructor is called earlier than
    // the module's configuration would have been imported to the database.
    // In that case the $formatterPluginId is missing and it causes fatal
    // errors.
    $formatter_plugin_id = $config_factory
      ->get('apigee_edge_debug.settings')
      ->get('formatter');
    if ($formatter_plugin_id) {
      $this->formatter = $debug_message_formatter_plugin
        ->createInstance($formatter_plugin_id);
    }
    $this->currentUser = $currentUser;
    $this->moduleHandler = $module_handler;
    $this->messenger = $messenger;
  }

  /**
   * {@inheritdoc}
   */
  public function __invoke() {
    return function ($handler) {
      return function (RequestInterface $request, array $options) use ($handler) {

        // If devel kint module is enabled and the user has devel kint permission.
        if ($this->moduleHandler
          ->moduleExists('kint') && $this->currentUser
          ->hasPermission('access kint')) {

          // If the formatter has been initialized yet then do nothing.
          if (!$this->formatter) {
            return $handler($request, $options);
          }
          $formatter = $this->formatter;
          $rest_call = [];
          if (isset($options[RequestOptions::ON_STATS])) {
            $next = $options[RequestOptions::ON_STATS];
          }
          else {
            $next = function (TransferStats $stats) {
            };
          }
          $options[RequestOptions::ON_STATS] = function (TransferStats $stats) use ($request, $next, $formatter) {
            $this->messenger
              ->addStatus(t('<h3>Edge Calls</h3>'));
            $level = LogLevel::DEBUG;

            // Do not modify the original request object in the subsequent calls.
            $request_clone = clone $request;
            $rest_call['Request'] = $formatter
              ->formatRequest($request_clone);
            if ($stats
              ->hasResponse()) {

              // Do not modify the original response object in the subsequent calls.
              $response_clone = clone $stats
                ->getResponse();
              $rest_call['Response'] = $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);
                }
              }
              $rest_call['Error'] = $error;
            }
            $next($stats);
            $rest_call['Time Elapsed'] = $formatter
              ->formatStats($stats);
            $rest_call['Severity'] = isset($level) ? $level : '';
            ksm($rest_call);
          };
        }
        return $handler($request, $options);
      };
    };
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DevelKintApiClientProfiler::$currentUser private property The currently logged-in user.
DevelKintApiClientProfiler::$formatter private property The debug message formatter plugin.
DevelKintApiClientProfiler::$messenger private property The messenger service.
DevelKintApiClientProfiler::$moduleHandler private property The module handler.
DevelKintApiClientProfiler::__construct public function DevelKintApiClientProfiler constructor.
DevelKintApiClientProfiler::__invoke public function