You are here

public function UsageEventSubscriber::onUsageRegister in Bynder 8.3

Same name and namespace in other branches
  1. 4.0.x modules/bynder_usage/src/EventSubscriber/UsageEventSubscriber.php \Drupal\bynder_usage\EventSubscriber\UsageEventSubscriber::onUsageRegister()

Triggers when a usage is registered (create or update) for a Bynder asset.

Parameters

\Drupal\entity_usage\Events\EntityUsageEvent $event: The event to process.

File

modules/bynder_usage/src/EventSubscriber/UsageEventSubscriber.php, line 185

Class

UsageEventSubscriber
Listens for the usage events from Entity Usage module.

Namespace

Drupal\bynder_usage\EventSubscriber

Code

public function onUsageRegister(EntityUsageEvent $event) {
  $mediaInfo = $this
    ->getUsageEventMediainformation($event);
  if (isset($mediaInfo)) {
    try {

      // Add a usage only once if the count is positive and there are no
      // usages registered on this URI.
      if ($event
        ->getCount() > 0) {
        $usage_url = $mediaInfo['url'];
        if (!$this
          ->hasRemoteUsageByUri($mediaInfo['mediaId'], $usage_url
          ->toString())) {
          $this->bynderApi
            ->addAssetUsage($mediaInfo['mediaId'], $usage_url, date(DATE_ISO8601, \Drupal::time()
            ->getRequestTime()), 'Added asset by user ' . \Drupal::currentUser()
            ->getAccountName() . '.');
        }
      }
      else {
        $this->bynderApi
          ->removeAssetUsage($mediaInfo['mediaId'], $mediaInfo['url']
          ->toString());
      }
    } catch (RequestException $e) {
      watchdog_exception('bynder', $e);
      (new UnableToAddUsageException($e
        ->getMessage()))
        ->logException()
        ->displayMessage();
    }
  }
}