EntityShareClientCommands.php in Entity Share 8.3
Same filename and directory in other branches
Namespace
Drupal\entity_share_client\CommandsFile
modules/entity_share_client/src/Commands/EntityShareClientCommands.phpView source
<?php
declare (strict_types=1);
namespace Drupal\entity_share_client\Commands;
use Consolidation\SiteAlias\SiteAliasManagerAwareInterface;
use Consolidation\SiteAlias\SiteAliasManagerAwareTrait;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\entity_share_client\Service\EntityShareClientCliService;
use Drupal\entity_share_client\Service\RemoteManagerInterface;
use Drush\Commands\DrushCommands;
use Drush\Drush;
/**
* Class EntityShareClientCommands.
*
* These are the Drush >= 9 commands.
*
* @package Drupal\entity_share_client\Commands
*/
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();
}
}
}
Classes
Name | Description |
---|---|
EntityShareClientCommands | Class EntityShareClientCommands. |