You are here

protected function StatusReportController::getWebhooksPageSection in Acquia Content Hub 8.2

Returns the Webhooks page section.

Return value

array Renderable array.

Throws

\Exception

1 call to StatusReportController::getWebhooksPageSection()
StatusReportController::statusReportPage in modules/acquia_contenthub_publisher/src/Controller/StatusReportController.php
Renders "Status Report" page.

File

modules/acquia_contenthub_publisher/src/Controller/StatusReportController.php, line 87

Class

StatusReportController
Controller for Status Reports.

Namespace

Drupal\acquia_contenthub_publisher\Controller

Code

protected function getWebhooksPageSection($page) {
  $client_entities = $this
    ->getClientEntities($page);
  $returned_subscribers = $client_entities['results'];
  $total_subscribers = $client_entities['total'];
  $total_pages = $client_entities['total_pages'];
  $current_start = $page * self::LIMIT + 1;

  // @todo Remove condition once 8.8 is lowest supported version.
  // Keep the logic within the if statement, remove the else.
  if (!is_null($this->pagerManager)) {
    $this->pagerManager
      ->createPager($total_subscribers, self::LIMIT);
  }
  else {

    // Global function needed for pager.
    pager_default_initialize($total_subscribers, self::LIMIT);
  }
  $content['#attached']['library'][] = 'acquia_contenthub_publisher/acquia_contenthub_publisher';
  if ($total_subscribers > 0) {
    $content['clients_subheader'] = [
      '#type' => 'html_tag',
      '#tag' => 'h3',
      '#value' => $this
        ->t('Showing @start - @current_total of @total @label', [
        '@start' => $current_start,
        '@current_total' => count($returned_subscribers) - 1 + $current_start,
        '@total' => $total_subscribers,
        '@label' => $total_subscribers > 1 ? 'clients' : 'client',
      ]),
    ];
  }
  $content['pager'] = [
    '#type' => 'pager',
    '#quantity' => $total_pages,
  ];
  $content['clients_table'] = [
    '#type' => 'table',
    '#header' => [
      'name' => $this
        ->t('Client'),
      'type' => $this
        ->t('Type'),
      'uuid' => $this
        ->t('Webhook Domain'),
      'status' => $this
        ->t('Status'),
      'percent_exported' => $this
        ->t('Percent Exported'),
      'percent_imported' => $this
        ->t('Percent Imported'),
      'updated' => $this
        ->t('Last Updated'),
      'details' => $this
        ->t('Details'),
    ],
    '#empty' => $this
      ->t('No clients found.'),
  ];
  foreach ($returned_subscribers as $key => $client) {
    $type = $this
      ->getClientType($client['attributes']);
    $settings = $client['metadata']['settings'] ?? [];
    $webhook_uuid = $settings['webhook']['uuid'] ?? 'Not Registered';
    $webhook_url = $settings['webhook']['settings_url'] ?? 'Not Registered';
    $status = $this
      ->t('<span title="@status_title">Not Available</span>', [
      '@status_title' => 'No syndication data found.',
    ]);
    $percent_exported = $this
      ->t('<span title="@export_title">Not Available</span>', [
      '@export_title' => 'No data found. Only sites with the acquia_contenthub_publisher module enabled can export content.',
    ]);
    $percent_imported = $this
      ->t('<span title="@import_title">Not Available</span>', [
      '@import_title' => 'No data found. Only sites with the acquia_contenthub_subscriber module enabled can import content.',
    ]);
    $last_updated = $this
      ->t('<span title="@updated_title">Not Available</span>', [
      '@updated_title' => 'No syndication data found.',
    ]);

    // If this client has no interests, stop processing.
    try {
      $interests = $this->client
        ->getInterestsByWebhook($webhook_uuid);
    } catch (\Exception $e) {
      $interests = [];
    }
    if (!empty($interests)) {
      $status = !empty($client['metadata']['metrics']) ? $this
        ->getClientStatus($client['metadata']['metrics']) : 'Not Found';
      $client_progress = $this
        ->calculateProgress($client, $interests, $type);
      if (!empty($client_progress['publisher'])) {
        $percent_exported = $client_progress['publisher'];
      }
      if (!empty($client_progress['subscriber'])) {
        $percent_imported = $client_progress['subscriber'];
      }
      $last_updated = !empty($client['metadata']['metrics']) ? $this
        ->getLastUpdatedTime($client['metadata']['metrics']) : 'Not Found';
    }
    $url = Url::fromRoute('acquia_contenthub_publisher.single_status', [
      'uuid' => $client['uuid'],
    ], []);
    $link = Link::fromTextAndUrl('More Details', $url)
      ->toString();

    // Build the row.
    $content['clients_table'][] = $this
      ->buildClientEntityTableRow($client, $settings['name'], $type, $webhook_uuid, $webhook_url, $status, $percent_exported, $percent_imported, $last_updated, $link);
  }
  return $content;
}