class WebhooksCommands in Webhooks 8
The webhooks Drush commandfile.
Hierarchy
- class \Drupal\webhooks\Commands\WebhooksCommands extends \Drush\Commands\DrushCommands uses StringTranslationTrait
Expanded class hierarchy of WebhooksCommands
1 string reference to 'WebhooksCommands'
1 service uses WebhooksCommands
File
- src/
Commands/ WebhooksCommands.php, line 16
Namespace
Drupal\webhooks\CommandsView source
class WebhooksCommands extends DrushCommands {
use StringTranslationTrait;
/**
* Webhook service.
*
* @var \Drupal\webhooks\WebhooksService
*/
protected $webhookService;
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Webhooks Commands constructor.
*
* @param \Drupal\webhooks\WebhooksService $webhookService
* The webhook service.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
* The entity type manager.
*/
public function __construct(WebhooksService $webhookService, EntityTypeManagerInterface $entityTypeManager) {
$this->webhookService = $webhookService;
$this->entityTypeManager = $entityTypeManager;
}
/**
* Trigger all active webhooks configured for a given event.
*
* @param string $event
* Specify an event name to be triggered, e.g. 'entity:node:create',
* 'entity:user:update' or 'entity:taxonomy_term:delete'.
* @option payload
* JSON-encoded webhook payload for testing, e.g. '{"ping": "pong"}'.
* @option headers
* JSON-encoded webhook headers for testing, e.g. '{"X-Custom": "Header"}'.
* @option content_type
* Specify the content-type to send the webhook with, e.g. application/json
* or application/xml.
* @usage webhooks-trigger entity:node:create
* Usage description
*
* @command webhooks:trigger
* @aliases wt
*/
// @phpcs:ignore Drupal.Commenting.FunctionComment.ParamMissingDefinition,Drupal.Arrays.Array.LongLineDeclaration,Drupal.Commenting.FunctionComment.Missing
public function trigger($event, $options = [
'payload' => '',
'headers' => '',
'event' => 'default',
'content_type' => 'application/json',
]) {
$payload = (array) json_decode($options['payload'], TRUE);
$headers = (array) json_decode($options['headers'], TRUE);
$event = $event;
$content_type = $options['content_type'];
$webhook = new Webhook($payload, $headers, $event, $content_type);
// Trigger the webhook for all subscribers.
$this->webhookService
->triggerEvent($webhook, $event);
$this
->logger()
->success($this
->t('A webhook has been triggered with event @event and payload @payload', [
'@event' => $event,
'@payload' => $options['payload'],
]));
}
/**
* List webhooks.
*
* @option type
* Filter by webhook type, e.g. incoming, outgoing.
* @option status
* Filter by status, e.g. 0, 1.
* @usage webhooks-list
* Usage description
*
* @command webhooks:list
* @aliases wt
* @field-labels
* display_name: Name
* machine_name: Machine Name
* type: Type
* status: Status
* @default-fields display_name,machine_name,type,status
* @aliases pml,pm-list
* @filter-default-field display_name
* @return \Consolidation\OutputFormatters\StructuredData\RowsOfFields
* The rows of fields object.
*/
public function list($options = [
'type' => '',
'status' => NULL,
]) {
$query = $this->entityTypeManager
->getStorage('webhook_config')
->getQuery();
if (isset($options['status'])) {
$query
->condition('status', $options['status']);
}
if ($options['type']) {
$query
->condition('type', $options['type']);
}
$ids = $query
->execute();
$webhooks_configs = WebhookConfig::loadMultiple($ids);
/** @var \Drupal\webhooks\Entity\WebhookConfigInterface $webhooks_config */
foreach ($webhooks_configs as $webhooks_config) {
$rows[] = [
'display_name' => $webhooks_config
->label(),
'machine_name' => $webhooks_config
->id(),
'type' => $this
->t('@type', [
'@type' => $webhooks_config
->getType(),
]),
'status' => $webhooks_config
->status() ? $this
->t('active') : $this
->t('inactive'),
];
}
return new RowsOfFields((array) $rows);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
StringTranslationTrait:: |
protected | property | The string translation service. | 1 |
StringTranslationTrait:: |
protected | function | Formats a string containing a count of items. | |
StringTranslationTrait:: |
protected | function | Returns the number of plurals supported by a given language. | |
StringTranslationTrait:: |
protected | function | Gets the string translation service. | |
StringTranslationTrait:: |
public | function | Sets the string translation service to use. | 2 |
StringTranslationTrait:: |
protected | function | Translates a string to the current language or to a given language. | |
WebhooksCommands:: |
protected | property | The entity type manager. | |
WebhooksCommands:: |
protected | property | Webhook service. | |
WebhooksCommands:: |
public | function | List webhooks. | |
WebhooksCommands:: |
public | function | ||
WebhooksCommands:: |
public | function | Webhooks Commands constructor. |