View source
<?php
namespace Drupal\cms_content_sync\Controller;
use Drupal\cms_content_sync\Entity\EntityStatus;
use Drupal\cms_content_sync\Entity\Flow;
use Drupal\cms_content_sync\PushIntent;
use Drupal\cms_content_sync\SyncIntent;
use Drupal\Core\Controller\ControllerBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
class PushEntities extends ControllerBase {
protected $operations = [];
protected $title;
protected $callback;
protected $_showSkipped = false;
protected $_skipUnpushed = false;
protected $_skippedUnpushed = [];
protected $_skippedNoFlow = [];
public function __construct($existing = null) {
$this->operations = $existing;
$this->title = t('Push content');
$this->callback = '\\Drupal\\cms_content_sync\\Controller\\PushEntities::batchFinished';
}
public static function create(ContainerInterface $container, $existing = null) {
return new PushEntities($existing);
}
public function skipUnpushed() {
$this->_skipUnpushed = true;
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;
}
public function getSkippedUnpushed($labelsOnly = false) {
return $labelsOnly ? $this
->getLabels($this->_skippedUnpushed) : $this->_skippedUnpushed;
}
public function getSkippedNoFlow($labelsOnly = false) {
return $labelsOnly ? $this
->getLabels($this->_skippedNoFlow) : $this->_skippedNoFlow;
}
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();
$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;
}
public function get() {
return $this->operations;
}
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;
}
public function setTitle($set) {
$this->title = $set;
return $this;
}
public function setCallback($set) {
$this->callback = $set;
return $this;
}
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;
}
public function isEmpty() {
return !count($this->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,
]));
}
}
public static function batch($flow_id, $entity_type_id, $entity_id, &$context) {
$message = 'Pushing...';
$results = [];
if (isset($context['results'])) {
$results = $context['results'];
}
$flow = Flow::getAll()[$flow_id];
$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;
}
protected function getLabels($entities) {
$result = [];
foreach ($entities as $entity) {
$result[] = $entity
->label();
}
return $result;
}
}