You are here

class GetFile in Acquia Content Hub 8.2

Gets files during preview in content as a service.

@package Drupal\acquia_contenthub_preview\EventSubscriber\HandleWebhook

Hierarchy

  • class \Drupal\acquia_contenthub_publisher\EventSubscriber\HandleWebhook\GetFile implements \Symfony\Component\EventDispatcher\EventSubscriberInterface uses HandleWebhookTrait

Expanded class hierarchy of GetFile

1 string reference to 'GetFile'
acquia_contenthub_publisher.services.yml in modules/acquia_contenthub_publisher/acquia_contenthub_publisher.services.yml
modules/acquia_contenthub_publisher/acquia_contenthub_publisher.services.yml
1 service uses GetFile
acquia_contenthub_publisher.handle_webhook.get_file in modules/acquia_contenthub_publisher/acquia_contenthub_publisher.services.yml
Drupal\acquia_contenthub_publisher\EventSubscriber\HandleWebhook\GetFile

File

modules/acquia_contenthub_publisher/src/EventSubscriber/HandleWebhook/GetFile.php, line 17

Namespace

Drupal\acquia_contenthub_publisher\EventSubscriber\HandleWebhook
View source
class GetFile implements EventSubscriberInterface {
  use HandleWebhookTrait;

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

  /**
   * The stream wrapper manager.
   *
   * @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface
   */
  protected $streamWrapperManager;

  /**
   * GetFile constructor.
   *
   * @param \Drupal\acquia_contenthub\ContentHubCommonActions $common
   *   The common actions object.
   * @param \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager
   *   The stream wrapper manager service.
   */
  public function __construct(ContentHubCommonActions $common, StreamWrapperManagerInterface $stream_wrapper_manager) {
    $this->common = $common;
    $this->streamWrapperManager = $stream_wrapper_manager;
  }

  /**
   * {@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();
    $client = $event
      ->getClient();
    $settings = $client
      ->getSettings();
    $client_uuid = $settings
      ->getUuid();
    if ('successful' !== $payload['status'] || empty($payload['uuid']) || 'getFile' !== $payload['crud'] || $payload['initiator'] === $client_uuid || empty($payload['cdf'])) {
      return;
    }
    $file_uri = $payload['cdf']['uri'];
    $file_scheme = $payload['cdf']['scheme'];
    if ($this->streamWrapperManager
      ->isValidScheme($file_scheme) && is_file($file_uri)) {
      $binary = new BinaryFileResponse($file_uri, 200, [], $file_scheme === 'private' ? FALSE : TRUE, 'inline');
      $response = $this
        ->getResponse($event, '', 200, $binary);
      $event
        ->setResponse($response);
      $event
        ->stopPropagation();
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
GetFile::$common protected property The common actions object.
GetFile::$streamWrapperManager protected property The stream wrapper manager.
GetFile::getSubscribedEvents public static function Returns an array of event names this subscriber wants to listen to.
GetFile::onHandleWebhook public function Handles webhook events.
GetFile::__construct public function GetFile constructor.
HandleWebhookTrait::getResponse protected function Handles webhook response.