<?php
namespace App\EventSubscriber;
use Doctrine\Common\EventSubscriber;
use Doctrine\Persistence\Event\LifecycleEventArgs;
use Doctrine\ORM\Event\PostFlushEventArgs;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use App\Entity\User;
use App\Entity\Log;
use App\Entity\LogJournal;
use App\Entity\Onglet;
use App\Entity\Notification;
use App\Entity\Alerte;
use App\Entity\EvenementResultat;
use App\Entity\Methode;
use App\Entity\MethodeEtape;
use App\Service\LogService;
use App\Service\NotificationService;
use App\Service\AlerteService;
use App\Service\AppService;
use App\Service\MineService;
use App\Enum\NotificationActionEnum;
class DoctrineSubscriber implements EventSubscriber
{
private $logs = [];
private $notifs = [];
public function __construct(TokenStorageInterface $tokenStorage,
MineService $res_service, AppService $appService, LogService $logService, NotificationService $notificationService, AlerteService $alerteService)
{
$this->appService = $appService;
$this->logService = $logService;
$this->tokenStorage = $tokenStorage;
$this->notificationService = $notificationService;
$this->alerteService = $alerteService;
$this->mineService = $res_service;
}
/**
* @return array
*/
public function getSubscribedEvents()
{
return array(
'prePersist',
'preUpdate',
'preRemove',
'preflush',
'postPersist',
'postFlush',
);
}
/**
* @param LifecycleEventArgs $args
*/
public function prePersist(LifecycleEventArgs $args)
{
if (method_exists($args->getEntity(), 'setCreatedBy')) {
if ($this->tokenStorage->getToken()) {
$user = $this->tokenStorage->getToken()->getUser();
if ($user instanceof User) {
$entity = $args->getEntity();
$entity->setCreatedBy($user);
}
}
}
// Timestampable
if (method_exists($args->getEntity(), 'setCreatedAt')) {
$entity = $args->getEntity();
$entity->setCreatedAt(new \DateTime());
}
// Alertes, Ajoute le numéro de version
if ($args->getEntity() instanceof Alerte) {
$entity = $args->getEntity();
$this->alerteService->prePersist($entity);
}
// Référence auto
$traits = class_uses($args->getEntity());
if(array_key_exists("App\Traits\Referenceable", $traits)) {
$entity = $args->getEntity();
$ref = $this->appService->getReferenceAuto($entity);
$entity->setReference($ref);
}// if
}
public function postPersist(LifecycleEventArgs $args) {
// Log
if (
!$args->getEntity() instanceof LogJournal
&& !$args->getEntity() instanceof Log
&& !$args->getEntity() instanceof Onglet //on retire les logs pour les onglets
) {
$newLog = $this->logService->createLogJournal($args, 'New');
$this->logs[] = $newLog;
}
//Notification a la création d'un user
if ($args->getEntity() instanceof User) {
$params_notif = [
"prospect" => null,
"mine" => null,
"libelle" => "Création utilisateur",
"contenu" => null,
"url" => null,
"user" => null,
];
$this->notificationService->addNotification($params_notif,NotificationActionEnum::NEW_USER);
}// if
//Notification, chaque notif génère une action suivant son type. envoi de mail ...
if ($args->getEntity() instanceof Notification) {
$this->notificationService->actionNotification($args->getEntity());
}
// Prospect
if ($args->getEntity() instanceof Prospect) {
if (!is_null($this->tokenStorage->getToken())) {
$user = $this->tokenStorage->getToken()->getUser();
$repo_methode = $args->getEntityManager()->getRepository(Methode::class);
$repo_etape = $args->getEntityManager()->getRepository(MethodeEtape::class);
}// if
}// if
// Prescripteur
if ($args->getEntity() instanceof Prescripteur) {
$repo_resultat = $args->getEntityManager()->getRepository(EvenementResultat::class);
$repo_methode = $args->getEntityManager()->getRepository(Methode::class);
$repo_etape = $args->getEntityManager()->getRepository(MethodeEtape::class);
}// if
}// function
/**
* @param LifecycleEventArgs $args
*/
public function preUpdate(LifecycleEventArgs $args)
{
// Log
if (
!$args->getEntity() instanceof LogJournal
&& !$args->getEntity() instanceof Log
&& !$args->getEntity() instanceof Onglet //on retire les logs pour les onglets
) {
$newLog = $this->logService->createLogJournal($args, 'Edit');
$this->logs[] = $newLog;
}
// Blamable
if (method_exists($args->getEntity(), 'setUpdatedBy')) {
if ($this->tokenStorage->getToken()) {
$user = $this->tokenStorage->getToken()->getUser();
if ($user instanceof User) {
$entity = $args->getEntity();
$entity->setUpdatedBy($user);
}
}
}
// Timestampable
if (method_exists($args->getEntity(), 'setUpdatedAt')) {
$entity = $args->getEntity();
$entity->setUpdatedAt(new \DateTime());
}
// Alertes: Numéro de version ...
if ($args->getEntity() instanceof Alerte) {
$entity = $args->getEntity();
$this->alerteService->preUpdate($entity);
}
}
public function preRemove(LifecycleEventArgs $args)
{
//Log
if (
!$args->getEntity() instanceof LogJournal
&& !$args->getEntity() instanceof Log
&& !$args->getEntity() instanceof Onglet //on retire les logs pour les onglets
) {
$newLog = $this->logService->createLogJournal($args, 'Remove');
$this->logs[] = $newLog;
}
}
public function postFlush(PostFlushEventArgs $args)
{
// Flush Logs
if (!empty($this->logs)) {
$em = $args->getEntityManager();
foreach ($this->logs as $log) {
$em->persist($log);
}
$this->logs = [];
$em->flush();
}
// Flush Notifs
if (!empty($this->notifs)) {
$em = $args->getEntityManager();
foreach ($this->notifs as $notif) {
$em->persist($notif);
}
$this->notifs = [];
$em->flush();
}
}
}