View source
<?php
declare (strict_types=1);
namespace Drupal\entity_share_client\Command;
use Drupal\Console\Annotations\DrupalCommand;
use Drupal\Console\Core\Command\Shared\CommandTrait;
use Drupal\Console\Core\Style\DrupalStyle;
use Drupal\entity_share_client\Service\EntityShareClientCliService;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputOption;
class PullCommand extends Command {
use CommandTrait;
protected $cliService;
protected $io;
public function __construct(EntityShareClientCliService $cliService) {
parent::__construct();
$this->cliService = $cliService;
}
protected function setupIo(InputInterface $input, OutputInterface $output) {
$this->io = new DrupalStyle($input, $output);
}
protected function getIo() {
return $this->io;
}
public function t($string, array $args = []) {
$c = 'commands.' . strtr($this
->getName(), [
':' => '.',
]) . '.messages.';
$translations = [
'Channel successfully pulled. Execution time @time ms.' => $c . 'success',
'There is no remote website configured with the id: @remote_id.' => $c . 'no_remote',
'There is no channel configured or accessible with the id: @channel_id.' => $c . 'no_channel',
'Beginning to import content from URL: @url' => $c . 'beginning_url_import',
'@number entities have been imported.' => $c . 'number_imported',
'Looking for new content in channel @channel' => $c . 'update_channel_lookup',
'Looking for updated content at URL: @url' => $c . 'update_page_lookup',
'Channel successfully pulled. Number of updated entities: @count, execution time: @time ms' => $c . 'update_success',
];
if (array_key_exists($string, $translations)) {
$string = $translations[$string];
}
return strtr($this
->trans($string), $args);
}
protected function configure() {
$this
->setName('entity_share_client:pull')
->setDescription($this
->trans('commands.entity_share_client.pull.description'))
->addArgument('remote_id', InputArgument::REQUIRED, $this
->trans('commands.entity_share_client.pull.arguments.remote_id'))
->addArgument('channel_id', InputArgument::REQUIRED, $this
->trans('commands.entity_share_client.pull.arguments.channel_id'))
->addOption('update', 'u', InputOption::VALUE_OPTIONAL, $this
->trans('commands.entity_share_client.pull.options.update'), FALSE);
}
protected function execute(InputInterface $input, OutputInterface $output) {
$this
->setupIo($input, $output);
try {
$update = $input
->getOption('update') !== FALSE;
if ($update) {
$this->cliService
->ioPullUpdates($input
->getArgument('remote_id'), $input
->getArgument('channel_id'), $this
->getIo(), [
$this,
't',
]);
}
else {
$this->cliService
->ioPull($input
->getArgument('remote_id'), $input
->getArgument('channel_id'), $this
->getIo(), [
$this,
't',
]);
}
} catch (\Exception $e) {
$this
->getIo()
->error($e
->getMessage());
}
}
}