<?php
namespace App\EventListener;
use App\Entity\ActionLog;
use App\Manager\TechnicalNoteManager;
use App\Service\ActionLogService;
use App\Service\ControlService;
use Doctrine\Persistence\ManagerRegistry;
use Pimcore\Event\Model\DataObjectEvent;
use Psr\Log\LoggerInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
class TechnicalNoteListener
{
private HttpClientInterface $client;
private TechnicalNoteManager $technicalNoteManager;
private LoggerInterface $logger;
private $shercoNetworkSync;
private ActionLogService $actionLogService;
private ControlService $controlService;
private $managerRegistry;
public function __construct($shercoNetworkSync, HttpClientInterface $client, TechnicalNoteManager $technicalNoteManager,
LoggerInterface $logger, ActionLogService $actionLogService, ControlService $controlService, ManagerRegistry $managerRegistry)
{
$this->shercoNetworkSync = $shercoNetworkSync;
$this->client = $client;
$this->technicalNoteManager = $technicalNoteManager;
$this->logger = $logger;
$this->actionLogService = $actionLogService;
$this->controlService = $controlService;
$this->managerRegistry = $managerRegistry;
}
public function onTechnicalNotePostAdd(DataObjectEvent $o)
{
$technicalNote = $o->getObject();
if ($this->technicalNoteManager->objectIsTechnicalNote($technicalNote)) {
$type = 'technicalNote';
if ($technicalNote->getType() != 'folder') {
if ($technicalNote->get("o_className") == "TechnicalNote") {
$statut = $technicalNote->isPublished() ? 'valide' : 'en-cours';
$this->actionLogService->logActionTechnicalNote($o, 'add', $type, $statut);
}
}
}
}
public function onTechnicalNotePostUpdate(DataObjectEvent $o)
{
$technicalNote = $o->getObject();
if ($technicalNote->getType() != 'folder') {
if ($technicalNote->get("o_className") == "TechnicalNote") {
if ($technicalNote->isPublished()) {
$this->actionLogService->logActionTechnicalNote($o, 'update', 'technicalNote', "valide");
} else {
$repository = $this->managerRegistry->getRepository(ActionLog::class);
$actionLog = $repository->findOneBy(['objectId' => $technicalNote->getId(), 'status' => "valide"], ['date' => 'desc']);
if (isset($actionLog) && $actionLog->getAction() != 'delete') {
$this->actionLogService->logActionTechnicalNote($o, 'delete', 'technicalNote', "valide");
} else {
$this->actionLogService->logActionTechnicalNote($o, 'update', 'technicalNote', "en-cours");
}
}
}
}
}
public function onTechnicalNotePostDelete(DataObjectEvent $o)
{
$technicalNote = $o->getObject();
if ($this->technicalNoteManager->objectIsTechnicalNote($technicalNote)) {
$type = 'technicalNote';
if ($technicalNote->getType() != 'folder') {
if ($technicalNote->get("o_className") == "TechnicalNote") {
if ($technicalNote->isPublished()) {
$repository = $this->managerRegistry->getRepository(ActionLog::class);
$actionLog = $repository->findOneBy(['objectId' => $technicalNote->getId(), 'status' => "valide"], ['date' => 'desc']);
if (isset($actionLog) && $actionLog->getAction() != 'delete') {
$this->actionLogService->logActionTechnicalNote($o, 'delete', $type, "valide");
}
} else {
$this->actionLogService->logActionTechnicalNote($o, 'delete', 'technicalNote', "en-cours");
}
}
}
}
}
}