Commit 3f0e2115 authored by Sebastien Rosset's avatar Sebastien Rosset

Use Doctrine listeners to monitor elements addition/edition/deletion

parent a26286c8
<?php
namespace Biopen\GeoDirectoryBundle\EventListener;
use Biopen\GeoDirectoryBundle\Document\Element;
use Biopen\GeoDirectoryBundle\Document\ElementStatus;
use Biopen\GeoDirectoryBundle\Services\WebhookService;
use Doctrine\ODM\MongoDB\Event\LifecycleEventArgs;
class WebhookListener
{
protected $webhookService;
public function __construct(WebhookService $webhookService)
{
$this->webhookService = $webhookService;
}
public function postPersist(LifecycleEventArgs $args)
{
$document = $args->getDocument();
if ($document instanceof Element)
{
// Only call webhooks if the element added is immediately visible
if( $document->isVisible() ) {
$this->webhookService->queue('add', $document);
}
}
}
public function postUpdate(LifecycleEventArgs $args)
{
$document = $args->getDocument();
if ($document instanceof Element)
{
$uow = $args->getDocumentManager()->getUnitOfWork();
$changeSet = $uow->getDocumentChangeSet($document);
// We check if the status is in the changeSet AND if there was a previous status
// (On addition, postPersist and postUpdate are both called so this avoids a duplicate)
if( isset($changeSet['status']) && isset($changeSet['status'][0]) ) {
if( $changeSet['status'][0] < ElementStatus::PendingModification && $changeSet['status'][1] >= ElementStatus::PendingModification ) {
// Call webhooks if the element was hidden and is now visible
$this->webhookService->queue('add', $document);
} else if( $changeSet['status'][0] >= ElementStatus::PendingModification && $changeSet['status'][1] < ElementStatus::PendingModification ) {
// Call webhooks if the element was visible and is now hidden
$this->webhookService->queue('delete', $document);
}
} else {
// We don't queue edit actions yet because postUpdate is called thousands of times on user addition (?)
// $this->webhookService->queue('edit', $document);
}
}
}
public function postRemove(LifecycleEventArgs $args)
{
$document = $args->getDocument();
if ($document instanceof Element)
{
// Only call webhooks if the element removed was visible
if( $document->isVisible() ) {
$this->webhookService->queue('delete', $document);
}
}
}
}
\ No newline at end of file
......@@ -11,11 +11,11 @@ services:
biopen.element_action_service:
class: Biopen\GeoDirectoryBundle\Services\ElementActionService
arguments: [ "@doctrine.odm.mongoDB.document_manager", "@security.context", "@biopen.mail_service", "@biopen.element_pending_service", "@biopen.webhook_service" ]
arguments: [ "@doctrine.odm.mongoDB.document_manager", "@security.context", "@biopen.mail_service", "@biopen.element_pending_service" ]
biopen.element_pending_service:
class: Biopen\GeoDirectoryBundle\Services\ElementPendingService
arguments: [ "@doctrine.odm.mongoDB.document_manager", "@security.context", "@biopen.mail_service", "@biopen.webhook_service" ]
arguments: [ "@doctrine.odm.mongoDB.document_manager", "@security.context", "@biopen.mail_service" ]
biopen.element_form_service:
class: Biopen\GeoDirectoryBundle\Services\ElementFormService
......@@ -41,6 +41,14 @@ services:
- { name: doctrine_mongodb.odm.event_listener, event: postUpdate }
- { name: doctrine_mongodb.odm.event_listener, event: postRemove }
biopen.webhook_listener:
class: Biopen\GeoDirectoryBundle\EventListener\WebhookListener
arguments: [ '@biopen.webhook_service' ]
tags:
- { name: doctrine_mongodb.odm.event_listener, event: postPersist, lazy: true } # lazy to avoid circular dependencies
- { name: doctrine_mongodb.odm.event_listener, event: postUpdate, lazy: true } # lazy to avoid circular dependencies
- { name: doctrine_mongodb.odm.event_listener, event: postRemove, lazy: true } # lazy to avoid circular dependencies
biopen.db_integrity:
class: Biopen\GeoDirectoryBundle\EventListener\DatabaseIntegrityWatcher
tags:
......
......@@ -42,13 +42,12 @@ class ElementActionService
/**
* Constructor
*/
public function __construct(DocumentManager $documentManager, SecurityContext $securityContext, MailService $mailService, ElementPendingService $elementPendingService, WebhookService $webhookService)
public function __construct(DocumentManager $documentManager, SecurityContext $securityContext, MailService $mailService, ElementPendingService $elementPendingService)
{
$this->em = $documentManager;
$this->securityContext = $securityContext;
$this->mailService = $mailService;
$this->elementPendingService = $elementPendingService;
$this->webhookService = $webhookService;
}
public function import($element, $sendMail = false, $message = null, $status = null)
......@@ -58,8 +57,6 @@ class ElementActionService
$element->setStatus($status);
if ($sendMail) $this->mailService->sendAutomatedMail('add', $element, $message);
$element->updateTimestamp();
// $this->webhookService->queue('add', $element);
}
public function add($element, $sendMail = true, $message = null)
......@@ -68,8 +65,6 @@ class ElementActionService
$element->setStatus(ElementStatus::AddedByAdmin);
if($sendMail) $this->mailService->sendAutomatedMail('add', $element, $message);
$element->updateTimestamp();
$this->webhookService->queue('add', $element);
}
public function edit($element, $sendMail = true, $message = null, $modifiedByOwner = false, $directModerationWithHash = false)
......@@ -87,8 +82,6 @@ class ElementActionService
$element->setStatus($status);
if (!$modifiedByOwner) $this->resolveReports($element, $message);
$element->updateTimestamp();
$this->webhookService->queue('edit', $element);
}
public function createPending($element, $editMode, $userEmail)
......@@ -118,8 +111,6 @@ class ElementActionService
$element->setStatus($newStatus);
$this->resolveReports($element, $message);
$element->updateTimestamp();
$this->webhookService->queue('delete', $element);
}
public function restore($element, $sendMail = true, $message = null)
......@@ -129,8 +120,6 @@ class ElementActionService
$this->resolveReports($element, $message);
if($sendMail) $this->mailService->sendAutomatedMail('add', $element, $message);
$element->updateTimestamp();
$this->webhookService->queue('add', $element);
}
public function resolveReports($element, $message = '', $addContribution = false)
......
......@@ -33,12 +33,11 @@ class ElementPendingService
/**
* Constructor
*/
public function __construct(DocumentManager $documentManager, SecurityContext $securityContext, MailService $mailService, WebhookService $webhookService)
public function __construct(DocumentManager $documentManager, SecurityContext $securityContext, MailService $mailService)
{
$this->em = $documentManager;
$this->securityContext = $securityContext;
$this->mailService = $mailService;
$this->webhookService = $webhookService;
}
// When element in added or modified by non admin, we go throw this function
......@@ -108,8 +107,6 @@ class ElementPendingService
private function acceptNewElement($element, $message)
{
$this->mailService->sendAutomatedMail('add', $element, $message);
$this->webhookService->queue('add', $element);
}
public function refuseNewElement($element)
......@@ -133,8 +130,6 @@ class ElementPendingService
}
$this->mailService->sendAutomatedMail('edit', $element, $message);
$this->webhookService->queue('edit', $element);
}
private function refuseModifiedElement($element)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment