You are here

public function Bynder::ensureMetadata in Bynder 8.3

Same name and namespace in other branches
  1. 8.2 src/Plugin/media/Source/Bynder.php \Drupal\bynder\Plugin\media\Source\Bynder::ensureMetadata()
  2. 4.0.x src/Plugin/media/Source/Bynder.php \Drupal\bynder\Plugin\media\Source\Bynder::ensureMetadata()

Ensures the given media entity has Bynder metadata information in place.

Parameters

\Drupal\media\MediaInterface $media: The media entity.

bool $force: (optional) By default, this will not attempt to check for updated metadata if there is local data available. Pass TRUE to always check for changed metadata.

Return value

bool TRUE if the metadata is ensured. Otherwise, FALSE.

1 call to Bynder::ensureMetadata()
Bynder::getMetadata in src/Plugin/media/Source/Bynder.php
Gets the value for a metadata attribute for a given media item.

File

src/Plugin/media/Source/Bynder.php, line 235

Class

Bynder
Provides media source plugin for Bynder.

Namespace

Drupal\bynder\Plugin\media\Source

Code

public function ensureMetadata(MediaInterface $media, $force = FALSE) {
  $media_uuid = $this
    ->getSourceFieldValue($media);
  if (!empty($this->metadata[$media_uuid]) && !$force) {
    return TRUE;
  }
  if (!$media
    ->hasField(BynderMetadataItem::METADATA_FIELD_NAME)) {
    $this->logger
      ->get('bynder')
      ->error('The media type @type must have a Bynder metadata field named "bynder_metadata".', [
      '@type' => $media
        ->bundle(),
    ]);
    return FALSE;
  }
  if (!$media
    ->get(BynderMetadataItem::METADATA_FIELD_NAME)
    ->isEmpty() && !$force) {
    $metadata = Json::decode($media
      ->get(BynderMetadataItem::METADATA_FIELD_NAME)->value);
    if (is_array($metadata)) {
      $this->metadata[$media_uuid] = $metadata;
      return TRUE;
    }
  }
  try {

    // Try to fetch data if no previous request failed with a timeout.
    if (!static::$timoutDetected || $force) {
      $media_uuid = $this
        ->getSourceFieldValue($media);
      $this->metadata[$media_uuid] = $this
        ->filterRemoteMetadata((array) $this->bynderApi
        ->getMediaInfo($media_uuid));
      if ($this
        ->hasMetadataChanged($media, $this->metadata[$media_uuid])) {
        $encoded_metadata = Json::encode($this->metadata[$media_uuid]);
        if (!$encoded_metadata) {
          $this->logger
            ->get('bynder')
            ->error('Unable to JSON encode the returned API response for the media UUID @uuid.', [
            '@uuid' => $media_uuid,
          ]);
          return FALSE;
        }
        $media
          ->set(BynderMetadataItem::METADATA_FIELD_NAME, $encoded_metadata)
          ->save();
        return TRUE;
      }
      return TRUE;
    }
  } catch (GuzzleException $e) {
    if ($e instanceof ConnectException) {
      $handler_context = $e
        ->getHandlerContext();
      if (isset($handler_context['errno']) && $handler_context['errno'] == 28) {
        static::$timoutDetected = TRUE;
      }
    }
    $this->logger
      ->get('bynder')
      ->error('Unable to fetch info about the asset represented by media @name (@id) with message @message.', [
      '@name' => $media
        ->label(),
      '@id' => $media
        ->id(),
      '@message' => $e
        ->getMessage(),
    ]);
  }
  return FALSE;
}