You are here

class EntityShareClientCommands in Entity Share 8.3

Same name and namespace in other branches
  1. 8 modules/entity_share_client/src/Commands/EntityShareClientCommands.php \Drupal\entity_share_client\Commands\EntityShareClientCommands
  2. 8.2 modules/entity_share_client/src/Commands/EntityShareClientCommands.php \Drupal\entity_share_client\Commands\EntityShareClientCommands

Class EntityShareClientCommands.

These are the Drush >= 9 commands.

@package Drupal\entity_share_client\Commands

Hierarchy

  • class \Drupal\entity_share_client\Commands\EntityShareClientCommands extends \Drush\Commands\DrushCommands implements \Consolidation\SiteAlias\SiteAliasManagerAwareInterface uses \Consolidation\SiteAlias\SiteAliasManagerAwareTrait

Expanded class hierarchy of EntityShareClientCommands

1 string reference to 'EntityShareClientCommands'
drush.services.yml in modules/entity_share_client/drush.services.yml
modules/entity_share_client/drush.services.yml
1 service uses EntityShareClientCommands
entity_share_client.commands in modules/entity_share_client/drush.services.yml
Drupal\entity_share_client\Commands\EntityShareClientCommands

File

modules/entity_share_client/src/Commands/EntityShareClientCommands.php, line 22

Namespace

Drupal\entity_share_client\Commands
View source
class EntityShareClientCommands extends DrushCommands implements SiteAliasManagerAwareInterface {
  use SiteAliasManagerAwareTrait;

  /**
   * The interoperability CLI service.
   *
   * @var \Drupal\entity_share_client\Service\EntityShareClientCliService
   */
  protected $cliService;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * The remote manager.
   *
   * @var \Drupal\entity_share_client\Service\RemoteManagerInterface
   */
  protected $remoteManager;

  /**
   * EntityShareClientCommands constructor.
   *
   * @param \Drupal\entity_share_client\Service\EntityShareClientCliService $cliService
   *   The CLI service which allows interoperability.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
   *   The entity type manager.
   * @param \Drupal\entity_share_client\Service\RemoteManagerInterface $remoteManager
   *   The remote manager.
   */
  public function __construct(EntityShareClientCliService $cliService, EntityTypeManagerInterface $entityTypeManager, RemoteManagerInterface $remoteManager) {
    parent::__construct();
    $this->cliService = $cliService;
    $this->entityTypeManager = $entityTypeManager;
    $this->remoteManager = $remoteManager;
  }

  /**
   * Pull a channel from a remote website.
   *
   * @param array $options
   *   Additional command options.
   *
   * @command entity-share-client:pull
   * @options remote-id Required. The remote website id to import from.
   * @options channel-id Required. The remote channel id to import.
   * @options import-config-id Required. The import config id to import with.
   * @usage drush entity-share-client:pull --remote-id=site_1 --channel-id=articles_en --import-config-id=default
   *   Pull a channel from a remote website. The "Include count in collection
   *   queries" option should be enabled on the server website. This option is
   *   provided by the JSON:API Extras module.
   */
  public function pullChannel(array $options = [
    'remote-id' => '',
    'channel-id' => '',
    'import-config-id' => '',
  ]) {

    // Validate options.
    $required_options = [
      'remote-id',
      'channel-id',
      'import-config-id',
    ];
    $missing_option = FALSE;
    foreach ($required_options as $required_option) {
      if (empty($options[$required_option])) {
        $missing_option = TRUE;
        $this
          ->logger()
          ->error(dt('Missing required option @option.', [
          '@option' => $required_option,
        ]));
      }
    }
    if (!$missing_option) {
      $this->cliService
        ->ioPull($options['remote-id'], $options['channel-id'], $options['import-config-id'], $this
        ->io(), 'dt');
    }
  }

  /**
   * Pull all channels from a remote website.
   *
   * @param string $remote_id
   *   The remote entity ID.
   * @param string $import_config_id
   *   The import config entity ID.
   *
   * @command entity-share-client:pull-all
   * @usage drush entity-share-client:pull-all site_1 default
   *   Pull all channels from a remote website. The "Include count in collection
   *   queries" option should be enabled on the server website. This option is
   *   provided by the JSON:API Extras module.
   */
  public function pullAllChannels(string $remote_id, string $import_config_id) : void {
    try {

      /** @var \Drupal\entity_share_client\Entity\RemoteInterface $remote */
      $remote = $this->entityTypeManager
        ->getStorage('remote')
        ->load($remote_id);
    } catch (\Exception $exception) {
      $this
        ->io()
        ->error('Impossible to load the remote website with the ID: ' . $remote_id);
    }
    if ($remote === NULL) {
      return;
    }
    $channels = $this->remoteManager
      ->getChannelsInfos($remote);
    if (empty($channels)) {
      $this
        ->io()
        ->warning('Channel list is empty.');
      return;
    }
    $selfRecord = $this
      ->siteAliasManager()
      ->getSelf();
    $args = [];
    $options = [
      'remote-id' => $remote_id,
      'channel-id' => '',
      'import-config-id' => $import_config_id,
    ];
    foreach (array_keys($channels) as $channel_id) {
      $this
        ->io()
        ->write('Synchronizing ' . $channel_id, TRUE);
      $options['channel-id'] = $channel_id;
      $sub_process = Drush::drush($selfRecord, 'entity-share-client:pull', $args, $options);
      $sub_process
        ->run();
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityShareClientCommands::$cliService protected property The interoperability CLI service.
EntityShareClientCommands::$entityTypeManager protected property The entity type manager.
EntityShareClientCommands::$remoteManager protected property The remote manager.
EntityShareClientCommands::pullAllChannels public function Pull all channels from a remote website.
EntityShareClientCommands::pullChannel public function Pull a channel from a remote website.
EntityShareClientCommands::__construct public function EntityShareClientCommands constructor.