You are here

class Report in Acquia Content Hub 8.2

Handles webhooks with a payload requesting site report.

@package Drupal\acquia_contenthub\EventSubscriber\HandleWebhook

Hierarchy

  • class \Drupal\acquia_contenthub\EventSubscriber\HandleWebhook\Report implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of Report

1 string reference to 'Report'
acquia_contenthub.services.yml in ./acquia_contenthub.services.yml
acquia_contenthub.services.yml
1 service uses Report
acquia_contenthub.handle_webhook.report in ./acquia_contenthub.services.yml
Drupal\acquia_contenthub\EventSubscriber\HandleWebhook\Report

File

src/EventSubscriber/HandleWebhook/Report.php, line 26

Namespace

Drupal\acquia_contenthub\EventSubscriber\HandleWebhook
View source
class Report implements EventSubscriberInterface {
  const PAYLOAD_REPORT = 'report';

  /**
   * The common contenthub actions object.
   *
   * @var \Drupal\acquia_contenthub\ContentHubCommonActions
   */
  protected $common;

  /**
   * The module extension list.
   *
   * @var \Drupal\Core\Extension\ModuleExtensionList
   */
  protected $moduleList;

  /**
   * Report constructor.
   *
   * @param \Drupal\acquia_contenthub\ContentHubCommonActions $common
   *   Common Actions.
   * @param \Drupal\Core\Extension\ModuleExtensionList $module_list
   *   List of modules.
   */
  public function __construct(ContentHubCommonActions $common, ModuleExtensionList $module_list) {
    $this->common = $common;
    $this->moduleList = $module_list;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[AcquiaContentHubEvents::HANDLE_WEBHOOK][] = [
      'onHandleWebhook',
      1000,
    ];
    return $events;
  }

  /**
   * Handles webhook events.
   *
   * @param \Drupal\acquia_contenthub\Event\HandleWebhookEvent $event
   *   The HandleWebhookEvent object.
   *
   * @throws \Exception
   */
  public function onHandleWebhook(HandleWebhookEvent $event) : void {
    $payload = $event
      ->getPayload();
    if ('successful' !== $payload['status'] || self::PAYLOAD_REPORT !== $payload['crud']) {
      return;
    }
    $response = $this
      ->getResponse($event, json_encode($this
      ->getReport()));
    $event
      ->setResponse($response);
    $event
      ->stopPropagation();
  }

  /**
   * Get Response method.
   *
   * @param \Drupal\acquia_contenthub\Event\HandleWebhookEvent $event
   *   Webhook event.
   * @param string $body
   *   Body of response.
   *
   * @todo Copied from preview. Move it a more generic class.
   *
   * @return \Psr\Http\Message\ResponseInterface
   *   Signed response.
   */
  protected function getResponse(HandleWebhookEvent $event, string $body) : ResponseInterface {
    $response = new GuzzleResponse(Response::HTTP_OK, [], $body);
    if (class_exists(DiactorosFactory::class)) {
      $httpMessageFactory = new DiactorosFactory();
    }
    else {
      $httpMessageFactory = new PsrHttpFactory(new ServerRequestFactory(), new StreamFactory(), new UploadedFileFactory(), new ResponseFactory());
    }
    $psr7_request = $httpMessageFactory
      ->createRequest($event
      ->getRequest());
    $signer = new ResponseSigner($event
      ->getKey(), $psr7_request);
    $signedResponse = $signer
      ->signResponse($response);
    return $signedResponse;
  }

  /**
   * Get report of site modules and db update status.
   *
   * @return array
   *   Array of modules and db update status.
   */
  protected function getReport() : array {
    return [
      'modules' => $this->moduleList
        ->getAllInstalledInfo(),
      'updatedb-status' => $this->common
        ->getUpdateDbStatus(),
    ];
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Report::$common protected property The common contenthub actions object.
Report::$moduleList protected property The module extension list.
Report::getReport protected function Get report of site modules and db update status.
Report::getResponse protected function Get Response method.
Report::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
Report::onHandleWebhook public function Handles webhook events.
Report::PAYLOAD_REPORT constant
Report::__construct public function Report constructor.