Skip to content

Commit

Permalink
Merge pull request #8 from aligent/feature/CreateCustomWebhookEvent
Browse files Browse the repository at this point in the history
Create new custom event
  • Loading branch information
pasqualinibruno authored Jul 20, 2023
2 parents 29d261d + 6c4a130 commit 1571ca2
Show file tree
Hide file tree
Showing 12 changed files with 174 additions and 100 deletions.
1 change: 1 addition & 0 deletions Async/Topics.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Topics
const WEBHOOK_ENTITY_CREATE = 'aligent.webhook.entity.create';
const WEBHOOK_ENTITY_UPDATE = 'aligent.webhook.entity.update';
const WEBHOOK_ENTITY_DELETE = 'aligent.webhook.entity.delete';
const WEBHOOK_ENTITY_CUSTOM = 'aligent.webhook.entity.custom';

const EVENT_MAP = [
WebhookConfigProvider::UPDATE => self::WEBHOOK_ENTITY_UPDATE,
Expand Down
30 changes: 11 additions & 19 deletions Async/WebhookEntityProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,12 @@ class WebhookEntityProcessor extends AbstractRetryableProcessor implements Topic
Topics::WEBHOOK_ENTITY_UPDATE => WebhookConfigProvider::UPDATE,
Topics::WEBHOOK_ENTITY_DELETE => WebhookConfigProvider::DELETE,
Topics::WEBHOOK_ENTITY_CREATE => WebhookConfigProvider::CREATE,
Topics::WEBHOOK_ENTITY_CUSTOM => WebhookConfigProvider::CUSTOM,
];

/**
* @var WebhookConfigProvider
*/
protected $configProvider;

/**
* @var WebhookTransport
*/
protected $transport;

/**
* @var SerializerInterface
*/
protected $serializer;
protected WebhookTransport $transport;
protected SerializerInterface $serializer;
protected WebhookConfigProvider $configProvider;

/**
* @param SerializerInterface $serializer
Expand Down Expand Up @@ -82,7 +72,7 @@ public function setTransport(WebhookTransport $transport): WebhookEntityProcesso
/**
* @inheritDoc
*/
public function execute(MessageInterface $message)
public function execute(MessageInterface $message): string
{
$data = JSON::decode($message->getBody());
$topic = $message->getProperty(Config::PARAMETER_TOPIC_NAME);
Expand Down Expand Up @@ -119,6 +109,7 @@ public function execute(MessageInterface $message)
$this->logger->error(
$message,
[

'channelId' => $channel->getId(),
'channel' => $channel->getName(),
'topic' => $topic,
Expand All @@ -134,26 +125,27 @@ public function execute(MessageInterface $message)
/**
* @inheritDoc
*/
public static function getSubscribedTopics()
public static function getSubscribedTopics(): array
{
return [
Topics::WEBHOOK_ENTITY_CREATE,
Topics::WEBHOOK_ENTITY_DELETE,
Topics::WEBHOOK_ENTITY_UPDATE,
Topics::WEBHOOK_ENTITY_CUSTOM,
];
}

/**
* @param string $event
* @param array $data
* @return array
* @return array<string, mixed>
* @throws \Symfony\Component\Serializer\Exception\ExceptionInterface
*/
protected function buildPayload(string $event, array $data)
protected function buildPayload(string $event, array $data): array
{
$entity = $this->registry->getRepository($data['class'])->find($data['id']);

if ($event === WebhookConfigProvider::CREATE) {
if (in_array($event, [WebhookConfigProvider::CREATE, WebhookConfigProvider::CUSTOM])) {
$changeSet = [];
} else {
// extract all of the before values from the change set
Expand Down
54 changes: 36 additions & 18 deletions Form/Type/WebhookTransportSettingsType.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use Aligent\AsyncEventsBundle\Entity\WebhookTransport;
use Aligent\AsyncEventsBundle\Provider\WebhookConfigProvider;
use Aligent\AsyncEventsBundle\Provider\WebhookCustomEventsProviderInterface;
use Doctrine\Persistence\ManagerRegistry;
use Oro\Bundle\FormBundle\Form\Type\CollectionType;
use Oro\Bundle\FormBundle\Form\Type\OroChoiceType;
Expand All @@ -25,28 +26,35 @@
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Contracts\Translation\TranslatorInterface;

class WebhookTransportSettingsType extends AbstractType
{
/**
* @var ManagerRegistry
*/
protected $registry;
protected ManagerRegistry $registry;
protected TranslatorInterface $translator;
protected WebhookCustomEventsProviderInterface $customEventsProvider;

/**
* WebhookTransportSettingsType constructor.
* @param ManagerRegistry $registry
* @param TranslatorInterface $translator
* @param WebhookCustomEventsProviderInterface $customEventsProvider
*/
public function __construct(ManagerRegistry $registry)
{
public function __construct(
ManagerRegistry $registry,
TranslatorInterface $translator,
WebhookCustomEventsProviderInterface $customEventsProvider
) {
$this->registry = $registry;
$this->translator = $translator;
$this->customEventsProvider = $customEventsProvider;
}

/**
* @param FormBuilderInterface $builder
* @param array $options
* @param array<string, mixed> $options
* @return void
*/
public function buildForm(FormBuilderInterface $builder, array $options)
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add(
Expand Down Expand Up @@ -83,7 +91,10 @@ public function buildForm(FormBuilderInterface $builder, array $options)
OroChoiceType::class,
[
'required' => true,
'choices' => $this->getEventsList()
'choices' => $this->getEventsList(),
'choice_label' => function ($choice, $key) {
return $this->translator->trans($key);
}
]
)
->add(
Expand Down Expand Up @@ -117,9 +128,9 @@ public function buildForm(FormBuilderInterface $builder, array $options)
}

/**
* @return array
* @return array<string, string>
*/
protected function getEntityList()
protected function getEntityList(): array
{
$em = $this->registry->getManager();
$entities = [];
Expand All @@ -133,21 +144,28 @@ protected function getEntityList()
}

/**
* @return array
* @return array<string, string>
*/
protected function getEventsList()
protected function getEventsList(): array
{
/** @var array<string, string> $events */
$events = [];
$events[WebhookConfigProvider::CREATE] = WebhookConfigProvider::CREATE;
$events[WebhookConfigProvider::UPDATE] = WebhookConfigProvider::UPDATE;
$events[WebhookConfigProvider::DELETE] = WebhookConfigProvider::DELETE;
// [ translation for the event's name => event key (event key is saved in the db and is 16 char long ]
$events['aligent.async.transport.form.event.create'] = WebhookConfigProvider::CREATE;
$events['aligent.async.transport.form.event.update'] = WebhookConfigProvider::UPDATE;
$events['aligent.async.transport.form.event.delete'] = WebhookConfigProvider::DELETE;

foreach ($this->customEventsProvider->getCustomEvents() as $customEvent) {
$events[$customEvent->getTranslationName()] = $customEvent->getKey();
}

return $events;
}
/**
* @param OptionsResolver $resolver
* @return void
*/
public function configureOptions(OptionsResolver $resolver)
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefault('data_class', WebhookTransport::class);
}
Expand Down
4 changes: 2 additions & 2 deletions Integration/WebhookChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ class WebhookChannel implements ChannelInterface
/**
* @inheritDoc
*/
public function getLabel()
public function getLabel(): string
{
return 'aligent.webhook.channel.label';
return 'aligent.async.channel.label';
}
}
43 changes: 15 additions & 28 deletions Integration/WebhookTransport.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,20 @@

use Aligent\AsyncEventsBundle\Form\Type\WebhookTransportSettingsType;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Oro\Bundle\IntegrationBundle\Entity\Channel;
use Oro\Bundle\IntegrationBundle\Entity\Transport;
use Oro\Bundle\IntegrationBundle\Provider\TransportInterface;
use Oro\Bundle\SecurityBundle\Encoder\SymmetricCrypterInterface;
use Psr\Log\LoggerInterface;
use Psr\Http\Message\ResponseInterface;

class WebhookTransport implements TransportInterface
{
/**
* @var SymmetricCrypterInterface
*/
protected $encoder;

/**
* @var LoggerInterface
*/
protected $logger;

/**
* @var Channel
*/
protected $channel;

/**
* @var Client
*/
protected $client;
protected Client $client;
protected Channel $channel;
protected LoggerInterface $logger;
protected SymmetricCrypterInterface $encoder;

/**
* AntavoRestTransport constructor.
Expand All @@ -56,7 +43,7 @@ public function __construct(SymmetricCrypterInterface $encoder, LoggerInterface
/**
* @inheritDoc
*/
public function init(Transport $transportEntity)
public function init(Transport $transportEntity): void
{
$settings = $transportEntity->getSettingsBag();
$this->channel = $transportEntity->getChannel();
Expand Down Expand Up @@ -84,11 +71,11 @@ public function init(Transport $transportEntity)

/**
* @param string $method
* @param array $payload
* @return \Psr\Http\Message\ResponseInterface
* @throws \GuzzleHttp\Exception\GuzzleException
* @param array<string, mixed> $payload
* @return ResponseInterface
* @throws GuzzleException
*/
public function sendWebhookEvent($method = 'POST', $payload = [])
public function sendWebhookEvent(string $method = 'POST', array $payload = []): ResponseInterface
{
return $this->client->request(
$method,
Expand All @@ -102,23 +89,23 @@ public function sendWebhookEvent($method = 'POST', $payload = [])
/**
* @inheritDoc
*/
public function getLabel()
public function getLabel(): string
{
return 'aligent.webhook.transport.label';
return 'aligent.async.transport.label';
}

/**
* @inheritDoc
*/
public function getSettingsFormType()
public function getSettingsFormType(): string
{
return WebhookTransportSettingsType::class;
}

/**
* @inheritDoc
*/
public function getSettingsEntityFQCN()
public function getSettingsEntityFQCN(): string
{
return \Aligent\AsyncEventsBundle\Entity\WebhookTransport::class;
}
Expand Down
Loading

0 comments on commit 1571ca2

Please sign in to comment.