AcquiaContentHubPublisherAuditCommands.php in Acquia Content Hub 8.2
File
modules/acquia_contenthub_publisher/src/Commands/AcquiaContentHubPublisherAuditCommands.php
View source
<?php
namespace Drupal\acquia_contenthub_publisher\Commands;
use Drupal\acquia_contenthub_publisher\PublisherTracker;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Queue\QueueFactory;
use Drush\Commands\DrushCommands;
use Drush\Exceptions\UserAbortException;
class AcquiaContentHubPublisherAuditCommands extends DrushCommands {
use DependencySerializationTrait;
protected $queue;
protected $tracker;
public function __construct(PublisherTracker $tracker, QueueFactory $queue_factory) {
$this->queue = $queue_factory
->get('acquia_contenthub_publish_export');
$this->tracker = $tracker;
}
public function audit(string $entity_type_id = '') {
$publish = $this->input
->getOption('publish');
if ($publish) {
$warning_message = dt('Are you sure you want to republish entities to Content Hub?');
if ($this
->io()
->confirm($warning_message) == FALSE) {
throw new UserAbortException();
}
}
$status = strtolower($this->input
->getOption('status'));
$entities = $this
->getContentHubTrackedEntities($status, $entity_type_id);
$this
->output()
->writeln(dt('<fg=green>Auditing entities with export status = @status</>', [
'@status' => $status,
]));
$batch = [
'title' => dt('Checks @status entities and compares them with Content Hub.', [
'@status' => $status,
]),
'operations' => [],
'finished' => '\\Drupal\\acquia_contenthub\\AuditTrackEntities::batchFinished',
];
$chunks = array_chunk($entities, 50);
foreach ($chunks as $chunk) {
$batch['operations'][] = [
'\\Drupal\\acquia_contenthub\\AuditTrackEntities::batchProcess',
[
$chunk,
$publish,
'publisher_audit',
],
];
}
batch_set($batch);
drush_backend_batch_process();
}
private function getContentHubTrackedEntities(string $status, $entity_type_id = '') : array {
switch ($status) {
case PublisherTracker::QUEUED:
if ($this->queue
->numberOfItems() > 0) {
throw new \Exception(dt('You cannot audit queued entities when the queue is not empty because you run the risk of re-queuing the same entities. Please retry when the queue is empty or delete the queued items manually'));
}
case PublisherTracker::CONFIRMED:
case PublisherTracker::EXPORTED:
$entities = $this->tracker
->listTrackedEntities($status, $entity_type_id);
break;
default:
throw new \Exception(dt('You can only use the following values for status: exported, confirmed, queued.'));
}
return $entities;
}
}