<?php
namespace Cdp\Transverse\Log\EventListener;
use Cdp\GroupeProjets\GroupeProjets\Entity\Fond\GroupeProjets;
use Cdp\GroupeProjets\Outils\Entity\Fond\Actions;
use Cdp\GroupeProjets\Outils\Entity\Fond\Outils;
use Cdp\GroupeProjets\Outils\Entity\Fond\Prets;
use Cdp\Transverse\Log\Entity\EntityHasWatchedLogInterface;
use Cdp\Transverse\PiecesJointes\Event\PostUploadEvent;
use Cdp\Transverse\PiecesJointes\Event\PreDeleteEvent;
use Cdp\Transverse\PiecesJointes\Model\Document;
use Cdp\Transverse\PiecesJointes\PiecesJointesEvents;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class LogPiecesJointesEventSubscriber extends AbstractLogEventSubscriber implements EventSubscriberInterface
{
// ✗ - modification document outils / action / pret bancaire - ou GP -> ??? n'existe pas
public static function getSubscribedEvents()
{
return [
PiecesJointesEvents::POST_UPLOAD => 'postUpload',
PiecesJointesEvents::PRE_DELETE => 'preDelete',
];
}
public function postUpload(PostUploadEvent $event)
{
$document = $event->getDocument();
$entity = $document->getTarget();
if ($entity instanceof EntityHasWatchedLogInterface) {
$this->handleEntityDocumentInsertion($entity, $document);
}
}
public function preDelete(PreDeleteEvent $event)
{
$document = $event->getDocument();
$entity = $document->getTarget();
if ($entity instanceof EntityHasWatchedLogInterface) {
$this->handleEntityDocumentDeletion($entity, $document);
}
}
protected function handleEntityDocumentInsertion(EntityHasWatchedLogInterface $entity, Document $document)
{
// - ajout document outils / action / pret bancaire - ou GP
switch (true) {
case $entity instanceof GroupeProjets:
$this->buildLog(
sprintf("Document '%s' ajouté sur le projet '%s'", $document->getInfo()->getFilename(), $entity),
[
$entity,
]
);
break;
case $entity instanceof Outils:
$this->buildLog(
sprintf("Document '%s' ajouté sur l'outil '%s'", $document->getInfo()->getFilename(), $entity),
[
$entity,
$entity->getGroupeProjet(),
]
);
break;
case $entity instanceof Actions:
$this->buildLog(
sprintf("Document '%s' ajouté sur l'action '%s'", $document->getInfo()->getFilename(), $entity),
[
$entity,
$entity->getGroupeProjet(),
]
);
break;
case $entity instanceof Prets:
$this->buildLog(
sprintf("Document '%s' ajouté sur le Pret '%s'", $document->getInfo()->getFilename(), $entity),
[
$entity,
$entity->getGroupeProjet(),
]
);
break;
}
}
protected function handleEntityDocumentDeletion(EntityHasWatchedLogInterface $entity, Document $document)
{
// - suppression document outils / action / pret bancaire - ou GP
switch (true) {
case $entity instanceof GroupeProjets:
$this->buildLog(
sprintf("Document '%s' supprimé sur le projet '%s'", $document->getInfo()->getFilename(), $entity),
[
$entity,
]
);
break;
case $entity instanceof Outils:
$this->buildLog(
sprintf("Document '%s' supprimé sur l'outil '%s'", $document->getInfo()->getFilename(), $entity),
[
$entity,
$entity->getGroupeProjet(),
]
);
break;
case $entity instanceof Actions:
$this->buildLog(
sprintf("Document '%s' supprimé sur l'action '%s'", $document->getInfo()->getFilename(), $entity),
[
$entity,
$entity->getGroupeProjet(),
]
);
break;
case $entity instanceof Prets:
$this->buildLog(
sprintf("Document '%s' supprimé sur le Pret '%s'", $document->getInfo()->getFilename(), $entity),
[
$entity,
$entity->getGroupeProjet(),
]
);
break;
}
}
}