You are here

class DumpAssets in Acquia Content Hub 8.2

Class DumpAssets.

Dumps a dependency list of untracked local entities of particular types.

@package Drupal\acquia_contenthub_subscriber\EventSubscriber\HandleWebhook

Hierarchy

  • class \Drupal\acquia_contenthub_subscriber\EventSubscriber\HandleWebhook\DumpAssets implements \Symfony\Component\EventDispatcher\EventSubscriberInterface

Expanded class hierarchy of DumpAssets

1 string reference to 'DumpAssets'
acquia_contenthub_subscriber.services.yml in modules/acquia_contenthub_subscriber/acquia_contenthub_subscriber.services.yml
modules/acquia_contenthub_subscriber/acquia_contenthub_subscriber.services.yml
1 service uses DumpAssets
acquia_contenthub_subscriber.handle_webhook.dump_assets in modules/acquia_contenthub_subscriber/acquia_contenthub_subscriber.services.yml
Drupal\acquia_contenthub_subscriber\EventSubscriber\HandleWebhook\DumpAssets

File

modules/acquia_contenthub_subscriber/src/EventSubscriber/HandleWebhook/DumpAssets.php, line 28

Namespace

Drupal\acquia_contenthub_subscriber\EventSubscriber\HandleWebhook
View source
class DumpAssets implements EventSubscriberInterface {

  /**
   * The subscriber tracker.
   *
   * @var \Drupal\acquia_contenthub_subscriber\SubscriberTracker
   */
  protected $tracker;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $manager;

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

  /**
   * ImportUpdateAssets constructor.
   *
   * @param \Drupal\acquia_contenthub_subscriber\SubscriberTracker $tracker
   *   The subscription tracker.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $manager
   *   The entity type manager.
   * @param \Drupal\acquia_contenthub\ContentHubCommonActions $common
   *   The common actions.
   */
  public function __construct(SubscriberTracker $tracker, EntityTypeManagerInterface $manager, ContentHubCommonActions $common) {
    $this->tracker = $tracker;
    $this->manager = $manager;
    $this->common = $common;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents() {
    $events[AcquiaContentHubEvents::HANDLE_WEBHOOK][] = [
      'onHandleWebhook',
      100,
    ];
    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();
    $client_uuid = $event
      ->getClient()
      ->getSettings()
      ->getUuid();
    if ('pending' !== $payload['status'] || 'dump' !== $payload['crud'] || $payload['initiator'] === $client_uuid || empty($payload['types']) || !is_array($payload['types'])) {
      return;
    }
    $entities = [];
    foreach ($payload['types'] as $entity_type) {
      try {
        $loaded = $this->manager
          ->getStorage($entity_type)
          ->loadMultiple();
      } catch (\Exception $e) {
        $loaded = [];
      }
      $uuids = [];
      foreach ($loaded as $entity) {
        $uuids[$entity
          ->uuid()] = $entity;
      }
      if ($uuids) {
        foreach ($this->tracker
          ->getUntracked(array_keys($uuids)) as $uuid) {
          $entities[$uuid] = $uuids[$uuid];
        }
      }
    }
    if ($entities) {
      $document = $this->common
        ->getLocalCdfDocument(...array_values($entities));
      $response = $this
        ->getResponse($event, $document
        ->toString());
      $event
        ->setResponse($response);
      $event
        ->stopPropagation();
    }
  }

  /**
   * Make a signed response specifically for the Handle webhook event.
   *
   * @param \Drupal\acquia_contenthub\Event\HandleWebhookEvent $event
   *   Handle webhook event.
   * @param string $body
   *   Response body.
   *
   * @return \Psr\Http\Message\ResponseInterface
   *   Signed response.
   */
  protected function getResponse(HandleWebhookEvent $event, string $body) {
    $response = new Response(SymfonyResponse::HTTP_OK, [], $body);
    if (class_exists(DiactorosFactory::class)) {
      $httpMessageFactory = new DiactorosFactory();
    }
    else {
      $httpMessageFactory = new PsrHttpFactory(new ServerRequestFactory(), new StreamFactory(), new UploadedFileFactory(), new ResponseFactory());
    }
    $psr7Request = $httpMessageFactory
      ->createRequest($event
      ->getRequest());
    $signer = new ResponseSigner($event
      ->getKey(), $psr7Request);
    $signedResponse = $signer
      ->signResponse($response);
    return $signedResponse;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DumpAssets::$common protected property The common actions.
DumpAssets::$manager protected property The entity type manager.
DumpAssets::$tracker protected property The subscriber tracker.
DumpAssets::getResponse protected function Make a signed response specifically for the Handle webhook event.
DumpAssets::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
DumpAssets::onHandleWebhook public function Handles webhook events.
DumpAssets::__construct public function ImportUpdateAssets constructor.