Merge pull request #3174 from aynsix/PHRAS-2739-incorporate-subdefwebhook

PHRAS-2739 #comment merge incorporate Phraseanet-plugin-SubdefWebhook into Phraseanet
This commit is contained in:
Nicolas Maillat
2020-03-18 10:59:55 +01:00
committed by GitHub
5 changed files with 110 additions and 4 deletions

View File

@@ -0,0 +1,75 @@
<?php
namespace Alchemy\Phrasea\Core\Event\Subscriber;
use Alchemy\Phrasea\Core\Event\Record\RecordEvents;
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 Silex\Application;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class WebhookSubdefEventSubscriber implements EventSubscriberInterface
{
private $app;
public function __construct(Application $app)
{
$this->app = $app;
}
public function onSubdefCreated(SubDefinitionCreatedEvent $event)
{
$eventData = [
'databox_id' => $event->getRecord()->getDataboxId(),
'record_id' => $event->getRecord()->getRecordId(),
'subdef' => $event->getSubDefinitionName()
];
$this->app['manipulator.webhook-event']->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->app['manipulator.webhook-event']->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->app['manipulator.webhook-event']->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'
];
}
}

View File

@@ -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\EventDispatcher;
class WebhookServiceProvider implements ServiceProviderInterface
{
@@ -58,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)
@@ -69,6 +79,6 @@ class WebhookServiceProvider implements ServiceProviderInterface
public function boot(Application $app)
{
// no-op
}
}

View File

@@ -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)
{

View File

@@ -0,0 +1,17 @@
<?php
namespace Alchemy\Phrasea\Webhook\Processor;
use Alchemy\Phrasea\Model\Entities\WebhookEvent;
class SubdefEventProcessor implements ProcessorInterface
{
public function process(WebhookEvent $event)
{
return [
'event' => $event->getName(),
'data' => $event->getData()
];
}
}

View File

@@ -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);
}
}