You are here

class PushEntities in CMS Content Sync 2.1.x

Same name and namespace in other branches
  1. 8 src/Controller/PushEntities.php \Drupal\cms_content_sync\Controller\PushEntities
  2. 2.0.x src/Controller/PushEntities.php \Drupal\cms_content_sync\Controller\PushEntities

Pull controller.

Note that this controller is also used by the draggableviews submodule.

Hierarchy

Expanded class hierarchy of PushEntities

1 file declares its use of PushEntities
cms_content_sync_draggableviews.module in modules/cms_content_sync_draggableviews/cms_content_sync_draggableviews.module

File

src/Controller/PushEntities.php, line 17

Namespace

Drupal\cms_content_sync\Controller
View source
class PushEntities extends ControllerBase {

  /**
   * @var null|array
   */
  protected $operations = [];

  /**
   * @var \Drupal\Core\StringTranslation\TranslatableMarkup
   */
  protected $title;

  /**
   * @var array|string
   */
  protected $callback;

  /**
   * @var bool
   */
  protected $_showSkipped = false;

  /**
   * @var bool
   */
  protected $_skipUnpushed = false;

  /**
   * @var array
   */
  protected $_skippedUnpushed = [];

  /**
   * @var array
   */
  protected $_skippedNoFlow = [];

  /**
   * PushEntities constructor.
   *
   * @param null|array $existing
   */
  public function __construct($existing = null) {
    $this->operations = $existing;
    $this->title = t('Push content');
    $this->callback = '\\Drupal\\cms_content_sync\\Controller\\PushEntities::batchFinished';
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, $existing = null) {
    return new PushEntities($existing);
  }

  /**
   * @return $this
   */
  public function skipUnpushed() {
    $this->_skipUnpushed = true;
    return $this;
  }

  /**
   * @return $this
   */
  public function showSkipped() {
    if ($count = count($this->_skippedNoFlow)) {
      $list = [
        '#theme' => 'item_list',
        '#items' => $this
          ->getSkippedNoFlow(true),
      ];
      \Drupal::messenger()
        ->addWarning(\Drupal::translation()
        ->translate("%count items were not pushed as they're not configured to be pushed: @items", [
        '%count' => $count,
        '@items' => \Drupal::service('renderer')
          ->render($list),
      ]));
    }
    if ($count = count($this->_skippedUnpushed)) {
      $list = [
        '#theme' => 'item_list',
        '#items' => $this
          ->getSkippedUnpushed(true),
      ];
      \Drupal::messenger()
        ->addStatus(\Drupal::translation()
        ->translate("%count items were not pushed as they weren't pushed before: @items", [
        '%count' => $count,
        '@items' => \Drupal::service('renderer')
          ->render($list),
      ]));
    }
    return $this;
  }

  /**
   * @param bool $labelsOnly
   *
   * @return array
   */
  public function getSkippedUnpushed($labelsOnly = false) {
    return $labelsOnly ? $this
      ->getLabels($this->_skippedUnpushed) : $this->_skippedUnpushed;
  }

  /**
   * @param bool $labelsOnly
   *
   * @return array
   */
  public function getSkippedNoFlow($labelsOnly = false) {
    return $labelsOnly ? $this
      ->getLabels($this->_skippedNoFlow) : $this->_skippedNoFlow;
  }

  /**
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *
   * @return $this
   */
  public function addEntity($entity) {
    $flows = PushIntent::getFlowsForEntity($entity, PushIntent::PUSH_FORCED, SyncIntent::ACTION_CREATE);
    if (!count($flows)) {
      $this->_skippedNoFlow[] = $entity;
      return $this;
    }
    $flow_id = $flows[0]
      ->id();

    /**
     * @var \Drupal\cms_content_sync\Entity\EntityStatus[] $entity_status
     */
    $entity_status = EntityStatus::getInfosForEntity($entity
      ->getEntityTypeId(), $entity
      ->uuid(), [
      'flow' => $flow_id,
    ]);
    if ($this->_skipUnpushed) {
      if (!count($entity_status) || !$entity_status[0]
        ->getLastPush()) {
        $this->_skippedUnpushed[] = $entity;
        return $this;
      }
    }
    $this
      ->add($flow_id, $entity
      ->getEntityTypeId(), $entity
      ->id());
    return $this;
  }

  /**
   * Get the operations that were already added. You can use them when instantiating this class again later to keep the
   * existing operations and add on top of them.
   *
   * @return null|array
   */
  public function get() {
    return $this->operations;
  }

  /**
   * @param string $flow_id
   * @param string $entity_type_id
   * @param int    $entity_id
   *
   * @return $this
   */
  public function add($flow_id, $entity_type_id, $entity_id) {
    $this->operations[] = [
      '\\Drupal\\cms_content_sync\\Controller\\PushEntities::batch',
      [
        $flow_id,
        $entity_type_id,
        $entity_id,
      ],
    ];
    return $this;
  }

  /**
   * @param string $set
   *
   * @return $this
   */
  public function setTitle($set) {
    $this->title = $set;
    return $this;
  }

  /**
   * @param array|string $set
   *
   * @return $this
   */
  public function setCallback($set) {
    $this->callback = $set;
    return $this;
  }

  /**
   * Start the actual batch operation.
   *
   * @param null|\Drupal\Core\Url $url
   *
   * @return null|\Symfony\Component\HttpFoundation\RedirectResponse
   */
  public function start($url = null) {
    $batch = [
      'title' => $this->title,
      'operations' => $this->operations,
      'finished' => $this->callback,
    ];
    batch_set($batch);
    if ($url) {
      return batch_process($url);
    }
    return null;
  }

  /**
   * Check if there actually are any operations to perform now.
   *
   * @return bool
   */
  public function isEmpty() {
    return !count($this->operations);
  }

  /**
   * Batch push finished callback.
   *
   * @param $success
   * @param $results
   * @param $operations
   */
  public static function batchFinished($success, $results, $operations) {
    $succeeded = count(array_filter($results));
    \Drupal::messenger()
      ->addMessage(t('%synchronized items have been pushed to your @repository.', [
      '@repository' => _cms_content_sync_get_repository_name(),
      '%synchronized' => $succeeded,
    ]));
    $failed = count($results) - $succeeded;
    if ($failed) {
      \Drupal::messenger()
        ->addMessage(t('%synchronized items have not been pushed to your @repository.', [
        '@repository' => _cms_content_sync_get_repository_name(),
        '%synchronized' => $failed,
      ]));
    }
  }

  /**
   * Batch push callback used by the following operations:
   * - Flow: Push All
   * - Content overview: Push changes
   * - Draggableviews: Push changes.
   *
   * @param string $flow_id
   * @param string $entity_type_id
   * @param int    $entity_id
   * @param array  $context
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   * @throws \GuzzleHttp\Exception\GuzzleException
   */
  public static function batch($flow_id, $entity_type_id, $entity_id, &$context) {
    $message = 'Pushing...';
    $results = [];
    if (isset($context['results'])) {
      $results = $context['results'];
    }

    /**
     * @var \Drupal\cms_content_sync\Entity\Flow $flow
     */
    $flow = Flow::getAll()[$flow_id];

    /**
     * @var \Drupal\Core\Entity\EntityTypeManager $entity_type_manager
     */
    $entity_type_manager = \Drupal::service('entity_type.manager');
    $entity = $entity_type_manager
      ->getStorage($entity_type_id)
      ->load($entity_id);
    try {
      $status = PushIntent::pushEntity($entity, PushIntent::PUSH_FORCED, SyncIntent::ACTION_CREATE, $flow);
    } catch (\Exception $exception) {
      \Drupal::messenger()
        ->addWarning(t('Item %label could not be pushed: %exception', [
        '%label' => $entity
          ->label(),
        '%exception' => $exception
          ->getMessage(),
      ]));
      $status = false;
    }
    $results[] = $status;
    $context['message'] = $message;
    $context['results'] = $results;
  }

  /**
   * @param $entities
   *
   * @return array
   */
  protected function getLabels($entities) {
    $result = [];
    foreach ($entities as $entity) {
      $result[] = $entity
        ->label();
    }
    return $result;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ControllerBase::$configFactory protected property The configuration factory.
ControllerBase::$currentUser protected property The current user service. 1
ControllerBase::$entityFormBuilder protected property The entity form builder.
ControllerBase::$entityTypeManager protected property The entity type manager.
ControllerBase::$formBuilder protected property The form builder. 2
ControllerBase::$keyValue protected property The key-value storage. 1
ControllerBase::$languageManager protected property The language manager. 1
ControllerBase::$moduleHandler protected property The module handler. 2
ControllerBase::$stateService protected property The state service.
ControllerBase::cache protected function Returns the requested cache bin.
ControllerBase::config protected function Retrieves a configuration object.
ControllerBase::container private function Returns the service container.
ControllerBase::currentUser protected function Returns the current user. 1
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::entityTypeManager protected function Retrieves the entity type manager.
ControllerBase::formBuilder protected function Returns the form builder service. 2
ControllerBase::keyValue protected function Returns a key/value storage collection. 1
ControllerBase::languageManager protected function Returns the language manager service. 1
ControllerBase::moduleHandler protected function Returns the module handler. 2
ControllerBase::redirect protected function Returns a redirect response object for the specified route.
ControllerBase::state protected function Returns the state storage service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 27
MessengerTrait::messenger public function Gets the messenger. 27
MessengerTrait::setMessenger public function Sets the messenger.
PushEntities::$callback protected property
PushEntities::$operations protected property
PushEntities::$title protected property
PushEntities::$_showSkipped protected property
PushEntities::$_skippedNoFlow protected property
PushEntities::$_skippedUnpushed protected property
PushEntities::$_skipUnpushed protected property
PushEntities::add public function
PushEntities::addEntity public function
PushEntities::batch public static function Batch push callback used by the following operations:
PushEntities::batchFinished public static function Batch push finished callback.
PushEntities::create public static function Instantiates a new instance of this class. Overrides ControllerBase::create
PushEntities::get public function Get the operations that were already added. You can use them when instantiating this class again later to keep the existing operations and add on top of them.
PushEntities::getLabels protected function
PushEntities::getSkippedNoFlow public function
PushEntities::getSkippedUnpushed public function
PushEntities::isEmpty public function Check if there actually are any operations to perform now.
PushEntities::setCallback public function
PushEntities::setTitle public function
PushEntities::showSkipped public function
PushEntities::skipUnpushed public function
PushEntities::start public function Start the actual batch operation.
PushEntities::__construct public function PushEntities constructor.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 4
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.