acquia_dam-1.0.0-rc1/src/Controller/VersionCheckController.php
src/Controller/VersionCheckController.php
<?php namespace Drupal\acquia_dam\Controller; use Drupal\acquia_dam\Client\AcquiaDamClientFactory; use Drupal\Core\Controller\ControllerBase; use Drupal\Core\Queue\QueueFactory; use Drupal\media\MediaInterface; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\HttpFoundation\RedirectResponse; /** * Controller for check updates custom operation. */ class VersionCheckController extends ControllerBase { /** * DAM client instance. * * @var \Drupal\acquia_dam\Client\AcquiaDamClient */ protected $damClient; /** * DAM asset update queue. * * @var \Drupal\Core\Queue\QueueInterface */ protected $assetUpdateQueue; /** * PreviewEntity constructor. * * @param \Drupal\acquia_dam\Client\AcquiaDamClientFactory $clientFactory * Client factory. * @param \Drupal\Core\Queue\QueueFactory $queueFactory * Queue factory. */ public function __construct(AcquiaDamClientFactory $clientFactory, QueueFactory $queueFactory) { $this->damClient = $clientFactory->getUserClient(); $this->assetUpdateQueue = $queueFactory->get('acquia_dam_asset_update'); } /** * {@inheritdoc} */ public static function create(ContainerInterface $container) { return new static( $container->get('acquia_dam.client.factory'), $container->get('queue') ); } /** * Checks for available updates. * * If there is update available, then it creates a queue item. * * @param \Drupal\media\MediaInterface $media * Media instance. * * @return \Symfony\Component\HttpFoundation\RedirectResponse * Redirect response. * * @throws \Drupal\Core\TypedData\Exception\MissingDataException */ public function checkVersionUpdate(MediaInterface $media): RedirectResponse { $asset_id = $media->get('acquia_dam_asset_id')->asset_id; try { $version_list = $this->damClient->getAssetVersionList($asset_id); } catch (\Exception $exception) { $this->messenger()->addWarning(sprintf( 'Cannot get asset version list from API. Asset id: %s, error: %s', $asset_id, $exception->getMessage()) ); return $this->redirect('view.dam_content_overview.page_1'); } $current_version = $media->get('acquia_dam_asset_id')->first()->getValue(); // We can assume, that the latest version is the last item of the array. usort($version_list['versions'], function ($a, $b) { return $a['dateLastEdited'] <=> $b['dateLastEdited']; }); $up_to_date_version = end($version_list['versions'])['uuid']; $label = $media->label(); $media_id = $media->id(); if ($current_version['version_id'] !== $up_to_date_version) { $this->assetUpdateQueue->createItem([ 'asset_id' => $asset_id, 'media_id' => $media_id, ]); $this->messenger()->addStatus(sprintf( 'Media entity has available newer version on DAM. Queue item created to get the up-to-date version. Media: %s, id: %s', $label, $media_id )); return $this->redirect('view.dam_content_overview.page_1'); } $this->messenger()->addStatus(sprintf( 'Current version is up-to-date for media entity: %s, id: %s.', $label, $media_id )); return $this->redirect('view.dam_content_overview.page_1'); } }