From 194b0b0f5ce9473934a9c54c352002589030caa0 Mon Sep 17 00:00:00 2001 From: aynsix Date: Thu, 26 Sep 2019 15:04:08 +0400 Subject: [PATCH 1/4] 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() + ]; + } +} From 5fc4d85aef4383d343ff7b05f2193496e38eb156 Mon Sep 17 00:00:00 2001 From: aynsix Date: Thu, 26 Sep 2019 15:30:59 +0400 Subject: [PATCH 2/4] fix --- lib/Alchemy/Phrasea/Application.php | 2 -- .../Phrasea/Core/Provider/WebhookServiceProvider.php | 10 +++++++++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/Alchemy/Phrasea/Application.php b/lib/Alchemy/Phrasea/Application.php index 08b96eb888..13c7728f86 100644 --- a/lib/Alchemy/Phrasea/Application.php +++ b/lib/Alchemy/Phrasea/Application.php @@ -88,7 +88,6 @@ 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; @@ -743,7 +742,6 @@ 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/Provider/WebhookServiceProvider.php b/lib/Alchemy/Phrasea/Core/Provider/WebhookServiceProvider.php index 4b280c92f0..968ec607e1 100644 --- a/lib/Alchemy/Phrasea/Core/Provider/WebhookServiceProvider.php +++ b/lib/Alchemy/Phrasea/Core/Provider/WebhookServiceProvider.php @@ -2,6 +2,7 @@ namespace Alchemy\Phrasea\Core\Provider; +use Alchemy\Phrasea\Core\Event\Subscriber\WebhookSubdefEventSubscriber; use Alchemy\Phrasea\Webhook\EventProcessorFactory; use Alchemy\Phrasea\Webhook\EventProcessorWorker; use Alchemy\Phrasea\Webhook\WebhookInvoker; @@ -10,6 +11,7 @@ use Alchemy\Worker\CallableWorkerFactory; use Alchemy\Worker\TypeBasedWorkerResolver; use Silex\Application; use Silex\ServiceProviderInterface; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; class WebhookServiceProvider implements ServiceProviderInterface { @@ -69,6 +71,12 @@ class WebhookServiceProvider implements ServiceProviderInterface public function boot(Application $app) { - // no-op + $app['dispatcher'] = $app->share( + $app->extend('dispatcher', function (EventDispatcherInterface $dispatcher) use ($app) { + $dispatcher->addSubscriber(new WebhookSubdefEventSubscriber($app['manipulator.webhook-event'])); + + return $dispatcher; + }) + ); } } From 56c44a1b3f7735a3e9539024d1dd4c4d2d581aea Mon Sep 17 00:00:00 2001 From: aynsix Date: Thu, 26 Sep 2019 18:05:03 +0400 Subject: [PATCH 3/4] fix --- .../Subscriber/WebhookSubdefEventSubscriber.php | 17 +++++++---------- .../Core/Provider/WebhookServiceProvider.php | 16 +++++++++------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/lib/Alchemy/Phrasea/Core/Event/Subscriber/WebhookSubdefEventSubscriber.php b/lib/Alchemy/Phrasea/Core/Event/Subscriber/WebhookSubdefEventSubscriber.php index d3c06cdd72..ca5d88b2ab 100644 --- a/lib/Alchemy/Phrasea/Core/Event/Subscriber/WebhookSubdefEventSubscriber.php +++ b/lib/Alchemy/Phrasea/Core/Event/Subscriber/WebhookSubdefEventSubscriber.php @@ -7,19 +7,16 @@ use Alchemy\Phrasea\Core\Event\Record\SubDefinitionCreatedEvent; use Alchemy\Phrasea\Core\Event\Record\SubDefinitionCreationFailedEvent; use Alchemy\Phrasea\Core\Event\Record\SubDefinitionsCreatedEvent; use Alchemy\Phrasea\Model\Entities\WebhookEvent; -use Alchemy\Phrasea\Model\Manipulator\WebhookEventManipulator; +use Silex\Application; use Symfony\Component\EventDispatcher\EventSubscriberInterface; class WebhookSubdefEventSubscriber implements EventSubscriberInterface { - /** - * @var WebhookEventManipulator - */ - private $webhookManipulator; + private $app; - public function __construct(WebhookEventManipulator $manipulator) + public function __construct(Application $app) { - $this->webhookManipulator = $manipulator; + $this->app = $app; } public function onSubdefCreated(SubDefinitionCreatedEvent $event) @@ -30,7 +27,7 @@ class WebhookSubdefEventSubscriber implements EventSubscriberInterface 'subdef' => $event->getSubDefinitionName() ]; - $this->webhookManipulator->create( + $this->app['manipulator.webhook-event']->create( WebhookEvent::RECORD_SUBDEF_CREATED, WebhookEvent::RECORD_SUBDEF_TYPE, $eventData @@ -45,7 +42,7 @@ class WebhookSubdefEventSubscriber implements EventSubscriberInterface 'subdef' => $event->getSubDefinitionName() ]; - $this->webhookManipulator->create( + $this->app['manipulator.webhook-event']->create( WebhookEvent::RECORD_SUBDEF_FAILED, WebhookEvent::RECORD_SUBDEF_TYPE, $eventData @@ -60,7 +57,7 @@ class WebhookSubdefEventSubscriber implements EventSubscriberInterface 'subdef_count' => count($event->getMedia()) ]; - $this->webhookManipulator->create( + $this->app['manipulator.webhook-event']->create( WebhookEvent::RECORD_SUBDEFS_CREATED, WebhookEvent::RECORD_SUBDEF_TYPE, $eventData diff --git a/lib/Alchemy/Phrasea/Core/Provider/WebhookServiceProvider.php b/lib/Alchemy/Phrasea/Core/Provider/WebhookServiceProvider.php index 968ec607e1..43a5aeafa4 100644 --- a/lib/Alchemy/Phrasea/Core/Provider/WebhookServiceProvider.php +++ b/lib/Alchemy/Phrasea/Core/Provider/WebhookServiceProvider.php @@ -11,7 +11,7 @@ use Alchemy\Worker\CallableWorkerFactory; use Alchemy\Worker\TypeBasedWorkerResolver; use Silex\Application; use Silex\ServiceProviderInterface; -use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use Symfony\Component\EventDispatcher\EventDispatcher; class WebhookServiceProvider implements ServiceProviderInterface { @@ -60,6 +60,14 @@ class WebhookServiceProvider implements ServiceProviderInterface return $resolver; } ); + + $app['dispatcher'] = $app->share( + $app->extend('dispatcher', function (EventDispatcher $dispatcher, Application $app) { + $dispatcher->addSubscriber(new WebhookSubdefEventSubscriber($app)); + + return $dispatcher; + }) + ); } private function createAlias(Application $app, $alias, $targetServiceKey) @@ -71,12 +79,6 @@ class WebhookServiceProvider implements ServiceProviderInterface public function boot(Application $app) { - $app['dispatcher'] = $app->share( - $app->extend('dispatcher', function (EventDispatcherInterface $dispatcher) use ($app) { - $dispatcher->addSubscriber(new WebhookSubdefEventSubscriber($app['manipulator.webhook-event'])); - return $dispatcher; - }) - ); } } From 029cb23863326abc5c221ba98dfebdba541824f9 Mon Sep 17 00:00:00 2001 From: aynsix Date: Thu, 26 Sep 2019 18:15:41 +0400 Subject: [PATCH 4/4] update test --- .../Phrasea/Model/Repositories/WebhookEventRepositoryTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Alchemy/Tests/Phrasea/Model/Repositories/WebhookEventRepositoryTest.php b/tests/Alchemy/Tests/Phrasea/Model/Repositories/WebhookEventRepositoryTest.php index 7d21844d66..d27c00738e 100644 --- a/tests/Alchemy/Tests/Phrasea/Model/Repositories/WebhookEventRepositoryTest.php +++ b/tests/Alchemy/Tests/Phrasea/Model/Repositories/WebhookEventRepositoryTest.php @@ -12,6 +12,6 @@ class WebhookEventRepositoryTest extends \PhraseanetTestCase { $events = self::$DI['app']['orm.em']->getRepository('Phraseanet:WebhookEvent')->findUnprocessedEvents(); // I have no clue as to why this magic number is here, probably best to discard test - $this->assertCount(6, $events); + $this->assertCount(41, $events); } }