Spaces:
No application file
No application file
File size: 1,651 Bytes
d2897cd |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
<?php
namespace Mautic\WebhookBundle\Event;
use Symfony\Component\Process\Exception\InvalidArgumentException;
use Symfony\Contracts\EventDispatcher\Event;
use Symfony\Contracts\Translation\TranslatorInterface;
class WebhookBuilderEvent extends Event
{
private array $events = [];
public function __construct(
private TranslatorInterface $translator
) {
}
/**
* Add an event for the event list.
*
* @param string $key - a unique identifier; it is recommended that it be namespaced i.e. lead.mytrigger
* @param array $event - can contain the following keys:
* 'label' => (required) what to display in the list
* 'description' => (optional) short description of event
*/
public function addEvent($key, array $event): void
{
if (array_key_exists($key, $this->events)) {
throw new InvalidArgumentException("The key, '$key' is already used by another webhook event. Please use a different key.");
}
$event['label'] = $this->translator->trans($event['label']);
$event['description'] = (isset($event['description'])) ? $this->translator->trans($event['description']) : '';
$this->events[$key] = $event;
}
/**
* Get webhook events.
*
* @return array
*/
public function getEvents()
{
static $sorted = false;
if (empty($sorted)) {
uasort($this->events, fn ($a, $b): int => strnatcasecmp(
$a['label'], $b['label']));
$sorted = true;
}
return $this->events;
}
}
|