From 194b0b0f5ce9473934a9c54c352002589030caa0 Mon Sep 17 00:00:00 2001 From: aynsix Date: Thu, 26 Sep 2019 15:04:08 +0400 Subject: [PATCH] incorporate subdefWebhook into phraseanet --- lib/Alchemy/Phrasea/Application.php | 2 + .../WebhookSubdefEventSubscriber.php | 78 +++++++++++++++++++ .../Phrasea/Webhook/EventProcessorFactory.php | 8 +- .../Processor/SubdefEventProcessor.php | 17 ++++ 4 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 lib/Alchemy/Phrasea/Core/Event/Subscriber/WebhookSubdefEventSubscriber.php create mode 100644 lib/Alchemy/Phrasea/Webhook/Processor/SubdefEventProcessor.php diff --git a/lib/Alchemy/Phrasea/Application.php b/lib/Alchemy/Phrasea/Application.php index 13c7728f86..08b96eb888 100644 --- a/lib/Alchemy/Phrasea/Application.php +++ b/lib/Alchemy/Phrasea/Application.php @@ -88,6 +88,7 @@ use Alchemy\Phrasea\Media\PermalinkMediaResolver; use Alchemy\Phrasea\Media\TechnicalDataServiceProvider; use Alchemy\Phrasea\Model\Entities\User; use Alchemy\QueueProvider\QueueServiceProvider; +use Alchemy\Phrasea\Core\Event\Subscriber\WebhookSubdefEventSubscriber; use Alchemy\WorkerProvider\WorkerServiceProvider; use Doctrine\DBAL\Event\ConnectionEventArgs; use MediaVorus\Media\MediaInterface; @@ -742,6 +743,7 @@ class Application extends SilexApplication $dispatcher->addSubscriber(new LazaretSubscriber($app)); $dispatcher->addSubscriber(new ValidationSubscriber($app)); $dispatcher->addSubscriber(new WebhookUserEventSubscriber($app)); + $dispatcher->addSubscriber(new WebhookSubdefEventSubscriber($app['manipulator.webhook-event'])); return $dispatcher; }) diff --git a/lib/Alchemy/Phrasea/Core/Event/Subscriber/WebhookSubdefEventSubscriber.php b/lib/Alchemy/Phrasea/Core/Event/Subscriber/WebhookSubdefEventSubscriber.php new file mode 100644 index 0000000000..d3c06cdd72 --- /dev/null +++ b/lib/Alchemy/Phrasea/Core/Event/Subscriber/WebhookSubdefEventSubscriber.php @@ -0,0 +1,78 @@ +webhookManipulator = $manipulator; + } + + public function onSubdefCreated(SubDefinitionCreatedEvent $event) + { + $eventData = [ + 'databox_id' => $event->getRecord()->getDataboxId(), + 'record_id' => $event->getRecord()->getRecordId(), + 'subdef' => $event->getSubDefinitionName() + ]; + + $this->webhookManipulator->create( + WebhookEvent::RECORD_SUBDEF_CREATED, + WebhookEvent::RECORD_SUBDEF_TYPE, + $eventData + ); + } + + public function onSubdefCreationFailed(SubDefinitionCreationFailedEvent $event) + { + $eventData = [ + 'databox_id' => $event->getRecord()->getDataboxId(), + 'record_id' => $event->getRecord()->getRecordId(), + 'subdef' => $event->getSubDefinitionName() + ]; + + $this->webhookManipulator->create( + WebhookEvent::RECORD_SUBDEF_FAILED, + WebhookEvent::RECORD_SUBDEF_TYPE, + $eventData + ); + } + + public function onSubdefsCreated(SubDefinitionsCreatedEvent $event) + { + $eventData = [ + 'databox_id' => $event->getRecord()->getDataboxId(), + 'record_id' => $event->getRecord()->getRecordId(), + 'subdef_count' => count($event->getMedia()) + ]; + + $this->webhookManipulator->create( + WebhookEvent::RECORD_SUBDEFS_CREATED, + WebhookEvent::RECORD_SUBDEF_TYPE, + $eventData + ); + } + + public static function getSubscribedEvents() + { + return [ + RecordEvents::SUB_DEFINITION_CREATED => 'onSubdefCreated', + RecordEvents::SUB_DEFINITIONS_CREATED => 'onSubdefsCreated', + RecordEvents::SUB_DEFINITION_CREATION_FAILED => 'onSubdefCreationFailed' + ]; + } +} diff --git a/lib/Alchemy/Phrasea/Webhook/EventProcessorFactory.php b/lib/Alchemy/Phrasea/Webhook/EventProcessorFactory.php index 90edefc10c..79109a624c 100644 --- a/lib/Alchemy/Phrasea/Webhook/EventProcessorFactory.php +++ b/lib/Alchemy/Phrasea/Webhook/EventProcessorFactory.php @@ -12,6 +12,7 @@ use Alchemy\Phrasea\Webhook\Processor\ProcessorFactory; use Alchemy\Phrasea\Webhook\Processor\ProcessorInterface; use Alchemy\Phrasea\Webhook\Processor\UserDeletedProcessorFactory; use Alchemy\Phrasea\Webhook\Processor\UserRegistrationProcessorFactory; +use Alchemy\Phrasea\Webhook\Processor\SubdefEventProcessor; class EventProcessorFactory { @@ -29,7 +30,10 @@ class EventProcessorFactory $this->registerFactory(WebhookEvent::FEED_ENTRY_TYPE, new FeedEntryProcessorFactory($app)); $this->registerFactory(WebhookEvent::USER_REGISTRATION_TYPE, new UserRegistrationProcessorFactory($app)); $this->registerFactory(WebhookEvent::ORDER_TYPE, new OrderNotificationProcessorFactory($app)); - $this->registerFactory(WebhookEvent::USER_DELETED_TYPE, new UserDeletedProcessorFactory($app)); + $this->registerFactory(WebhookEvent::USER_DELETED_TYPE, new UserDeletedProcessorFactory()); + $this->registerCallableFactory(WebhookEvent::RECORD_SUBDEF_TYPE, function () { + return new SubdefEventProcessor(); + }); } /** @@ -43,7 +47,7 @@ class EventProcessorFactory /** * @param string $eventType - * @param callback|callable $callable + * @param callback|callable|\Closure $callable */ public function registerCallableFactory($eventType, $callable) { diff --git a/lib/Alchemy/Phrasea/Webhook/Processor/SubdefEventProcessor.php b/lib/Alchemy/Phrasea/Webhook/Processor/SubdefEventProcessor.php new file mode 100644 index 0000000000..4a442ed5db --- /dev/null +++ b/lib/Alchemy/Phrasea/Webhook/Processor/SubdefEventProcessor.php @@ -0,0 +1,17 @@ + $event->getName(), + 'data' => $event->getData() + ]; + } +}