View source
<?php
namespace Drupal\acquia_contenthub\Commands;
use Drupal\acquia_contenthub\Client\ClientFactory;
use Drupal\acquia_contenthub\ContentHubConnectionManager;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drush\Commands\DrushCommands;
use Symfony\Component\Console\Helper\Table;
class AcquiaContentHubWebhookInterestCommands extends DrushCommands {
protected $clientFactory;
protected $client;
protected $webhook;
protected $webhookUrl;
protected $webhookUuid;
protected $connectionManager;
protected $config;
public function __construct(ClientFactory $client_factory, ContentHubConnectionManager $connection_manager, ConfigFactoryInterface $config_factory) {
$this->clientFactory = $client_factory;
$this->connectionManager = $connection_manager;
$this->config = $config_factory
->getEditable('acquia_contenthub.admin_settings');
}
private function findWebhook() : void {
$this->client = $this->clientFactory
->getClient();
if (!$this->client) {
throw new \Exception(dt('The Content Hub client is not connected so the webhook operations could not be performed.'));
}
$this->webhookUrl = $this
->formatWebhookUrl($this
->input()
->getOptions()['webhook_url'] ?? $this->client
->getSettings()
->getWebhook('url'));
$this->webhook = $this->client
->getWebHook($this->webhookUrl);
if (!$this->webhook) {
throw new \Exception(dt('The webhook is not available so the operation could not complete.'));
}
$this->webhookUuid = $this->webhook
->getUuid();
if (!$this->webhookUuid) {
throw new \Exception(dt('The webhook uuid is not available so the operation could not complete.'));
}
}
public function contenthubWebhookInterestsList() {
$this
->findWebhook();
$message = sprintf('Listing Interests for webhook %s', $this->webhookUrl);
$interests = $this->client
->getInterestsByWebhook($this->webhookUuid);
if (empty($interests)) {
$this
->output()
->writeln(dt('<fg=white;bg=red;options=bold;>No interests found for webhook @url</>', [
'@url' => $this->webhookUrl,
]));
return;
}
$interests = [
'Index' => 'Interest',
] + $interests;
$webhooks = array_merge([
'Webhook',
], array_fill(0, count($interests) - 1, $this->webhookUrl));
$this
->renderTable($message, TRUE, array_keys($interests), $interests, $webhooks);
}
public function contenthubWebhookInterestsAdd() {
$this
->findWebhook();
if (empty($this
->input()
->getOptions()['uuids'])) {
$this
->output()
->writeln(dt('<fg=white;bg=red;options=bold;>[error] Uuids are required to add interests.</>'));
return;
}
$uuids = explode(',', $this
->input()
->getOptions()['uuids']);
$send_update = $this->config
->get('send_contenthub_updates') ?? TRUE;
if ($send_update) {
$response = $this->client
->addEntitiesToInterestList($this->webhookUuid, $uuids);
if (empty($response)) {
return;
}
if (200 !== $response
->getStatusCode()) {
$this
->output()
->writeln('An error occurred and interests were not updated.');
return;
}
$this
->output()
->writeln("\nInterests updated successfully.\n");
}
$this
->contenthubWebhookInterestsList();
}
public function contenthubWebhookInterestsDelete() {
$this
->findWebhook();
if (empty($this
->input()
->getOptions()['uuids'])) {
$this
->output()
->writeln(dt('<fg=white;bg=red;options=bold;>[error] Uuids are required to delete interests.</>'));
return;
}
$uuids = explode(',', $this
->input()
->getOptions()['uuids']);
$this
->output()
->writeln("\n");
$send_update = $this->config
->get('send_contenthub_updates') ?? TRUE;
if ($send_update) {
foreach ($uuids as $uuid) {
$response = $this->client
->deleteInterest($uuid, $this->webhookUuid);
if (empty($response)) {
continue;
}
if (200 !== $response
->getStatusCode()) {
$this
->output()
->writeln(dt('An error occurred and the interest @uuid was not removed.', [
'@uuid' => $uuid,
]));
continue;
}
$this
->output()
->writeln(dt('Interest @uuid removed from webhook @webhook.', [
'@uuid' => $uuid,
'@webhook' => $this->webhookUrl,
]));
}
}
$this
->output()
->writeln("\n");
$this
->contenthubWebhookInterestsList();
}
public function syncInterestListWithTrackingTable() {
$this->connectionManager
->syncWebhookInterestListWithTrackingTables();
}
private function renderTable(string $message, bool $use_headers = FALSE, ...$cols) {
$rows_mapper = function (...$items) {
return $items;
};
if (!empty($message)) {
$this
->output()
->writeln($message);
}
$table = new Table($this
->output());
$headers = [];
if ($use_headers) {
foreach ($cols as &$col) {
$keys = array_keys($col);
$headers[] = $col[$keys[0]];
unset($col[$keys[0]]);
}
$table
->setHeaders($headers);
}
$rows = array_map($rows_mapper, ...$cols);
$table
->setRows($rows)
->render();
}
private function formatWebhookUrl(string $webhook_url) {
if (!strpos($webhook_url, 'acquia-contenthub/webhook')) {
$webhook_url .= 'acquia-contenthub/webhook';
}
return $webhook_url;
}
}