WebhookEnableForm.php in Acquia Content Hub 8.2
File
modules/acquia_contenthub_publisher/src/Form/Webhook/WebhookEnableForm.php
View source
<?php
namespace Drupal\acquia_contenthub_publisher\Form\Webhook;
use Acquia\ContentHubClient\Webhook;
use Drupal\acquia_contenthub\Client\ClientFactory;
use Drupal\acquia_contenthub_publisher\Form\SubscriptionManagerFormTrait;
use Drupal\Core\Form\ConfirmFormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Url;
use Symfony\Component\DependencyInjection\ContainerInterface;
class WebhookEnableForm extends ConfirmFormBase {
use SubscriptionManagerFormTrait;
protected $client;
protected $uuid;
public function __construct(ClientFactory $client_factory) {
$this->client = $client_factory
->getClient();
}
public static function create(ContainerInterface $container) {
return new static($container
->get('acquia_contenthub.client.factory'));
}
public function buildForm(array $form, FormStateInterface $form_state, $uuid = NULL) {
$this->uuid = $uuid;
return parent::buildForm($form, $form_state);
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$webhooks = $this->client
->getWebHooks();
$uuid = $this->uuid;
$webhook = current(array_filter($webhooks, function (Webhook $webhook) use ($uuid) {
return $webhook
->getUuid() === $uuid;
}));
if ($webhook) {
$response = $this->client
->addWebhook($webhook
->getUrl());
if (empty($response) || empty($response['success'])) {
$this
->messenger()
->addError($this
->t('Unable to re-enable webhook %uuid (%url). Error code: %code. Error message: %message', [
'%uuid' => $this->uuid,
'%url' => $webhook
->getUrl(),
'%code' => $response['error']['code'] ?? $this
->t('n/a'),
'%message' => $response['error']['message'] ?? $this
->t('n/a'),
]));
return;
}
$form_state
->setRedirect('acquia_contenthub.subscription_settings');
$this
->messenger()
->addStatus($this
->t('Webhook %uuid (%url) has been re-enabled.', [
'%uuid' => $this->uuid,
'%url' => $webhook
->getUrl(),
]));
return;
}
$this
->messenger()
->addError($this
->t('Failed to re-enable webhook %uuid. The webhook is not found.', [
'%uuid' => $this->uuid,
]));
}
public function getFormId() {
return 'acquia_contenthub_webhook_enable_form';
}
public function getCancelUrl() {
return new Url('acquia_contenthub.subscription_settings');
}
public function getQuestion() {
return t('Are you sure you want to re-enable webhook %uuid?', [
'%uuid' => $this->uuid,
]);
}
public function getDescription() {
return '';
}
}