config = $config;
parent::__construct();
$this->userManager = $userManager;
$this->cache = $cacheFactory->createDistributed(RelatedService::CACHE_RELATED);
$this->relatedService = $relatedService;
}
/**
* @return void
*/
protected function configure() {
parent::configure();
$this->setName('related:test')
->setHidden(!$this->config->getSystemValueBool('debug'))
->setDescription('returns related resource to a share')
->addArgument('userId', InputArgument::REQUIRED, 'user\'s point of view')
->addArgument('providerId', InputArgument::REQUIRED, 'Provider Id (ie. files)')
->addOption('clear-cache', '', InputOption::VALUE_NONE, 'clear cache')
->addOption('resource-type', '', InputOption::VALUE_REQUIRED, 'limit to a type of resources', '')
->addArgument('itemId', InputArgument::REQUIRED, 'Item Id');
}
/**
* @param InputInterface $input
* @param OutputInterface $output
*
* @return int
* @throws RelatedResourceProviderNotFound
*/
protected function execute(InputInterface $input, OutputInterface $output): int {
$userId = $input->getArgument('userId');
$providerId = $input->getArgument('providerId');
$itemId = $input->getArgument('itemId');
if ($input->getOption('clear-cache')) {
$this->cache->clear();
}
$user = $this->userManager->get($userId);
if (is_null($user)) {
throw new InvalidArgumentException('must specify a valid local user');
}
$userId = $user->getUID();
try {
/** @var CirclesManager $circleManager */
$circleManager = Server::get(CirclesManager::class);
} catch (ContainerExceptionInterface|AutoloadNotAllowedException $e) {
throw new Exception('Circles needs to be enabled');
}
$circleManager->startSession($circleManager->getLocalFederatedUser($userId));
$this->displayRecipients($providerId, $itemId);
$this->displayRelated($providerId, $itemId, $input->getOption('resource-type'), ($input->getOption('output') === 'json'));
return 0;
}
private function displayRecipients(string $providerId, string $itemId): void {
$result = $this->relatedService->getRelatedFromItem($providerId, $itemId);
$output = new ConsoleOutput();
$output->writeln('Title: ' . $result->getTitle());
$output->writeln('Group Shared: ' . ($result->isGroupShared() ? 'true' : 'false'));
$output->writeln('Virtual Group: ' . json_encode($result->getVirtualGroup()));
$output->writeln('Recipients: ' . json_encode($result->getRecipients()));
$output->writeln('');
}
private function displayRelated(string $providerId, string $itemId, string $resourceType, bool $json): void {
$result = $this->relatedService->getRelatedToItem($providerId, $itemId, -1, $resourceType);
$output = new ConsoleOutput();
if ($json) {
$output->writeln(json_encode($result, JSON_PRETTY_PRINT));
return;
}
$output = $output->section();
$table = new Table($output);
$table->setHeaders(
[
'Provider Id',
'Item Id',
'Title',
'Description',
'Score',
'Link'
]
);
$table->render();
foreach ($result as $entry) {
$table->appendRow(
[
$entry->getProviderId(),
$entry->getItemId(),
$entry->getTitle(),
$entry->getSubtitle(),
$entry->getScore(),
$entry->getUrl()
]
);
}
$output->writeln('');
}
}