<?php
namespace App\EventListener;
use App\Entity\ActionLog;
use App\Manager\EclateManager;
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 EclateListener
{
private HttpClientInterface $client;
private EclateManager $eclateManager;
private LoggerInterface $logger;
private $shercoNetworkSync;
private ActionLogService $actionLogService;
private ControlService $controlService;
private $managerRegistry;
public function __construct($shercoNetworkSync, HttpClientInterface $client, EclateManager $eclateManager,
LoggerInterface $logger, ActionLogService $actionLogService, ControlService $controlService, ManagerRegistry $managerRegistry)
{
$this->shercoNetworkSync = $shercoNetworkSync;
$this->client = $client;
$this->eclateManager = $eclateManager;
$this->logger = $logger;
$this->actionLogService = $actionLogService;
$this->controlService = $controlService;
$this->managerRegistry = $managerRegistry;
}
public function onEclatePostAdd(DataObjectEvent $o)
{
$eclate = $o->getObject();
if ($this->eclateManager->objectIsEclate($eclate)) {
$type = 'eclate';
if ($eclate->getType() != 'folder') {
if ($eclate->get("o_className") == "Eclate") {
$statut = $eclate->isPublished() ? 'valide' : 'en-cours';
$this->actionLogService->logActionEclate($o, 'add', $type, $statut);
}
}
}
}
public function onEclatePostUpdate(DataObjectEvent $o)
{
$eclate = $o->getObject();
if ($eclate->getType() != 'folder') {
if ($eclate->get("o_className") == "Eclate") {
if ( $this->controlService->controlEclateReference($eclate)) {
if ($eclate->isPublished()) {
$this->actionLogService->logActionEclate($o, 'update', 'eclate', "valide");
} else {
$repository = $this->managerRegistry->getRepository(ActionLog::class);
$actionLog = $repository->findOneBy(['objectId' => $eclate->getId(), 'status' => "valide"], ['date' => 'desc']);
if (isset($actionLog) && $actionLog->getAction() != 'delete') {
$this->actionLogService->logActionEclate($o, 'delete', 'eclate', "valide");
} else {
$this->actionLogService->logActionEclate($o, 'update', 'eclate', "en-cours");
}
}
}
}
}
}
public function onEclatePostDelete(DataObjectEvent $o)
{
$eclate = $o->getObject();
if ($this->eclateManager->objectIsEclate($eclate)) {
$type = 'eclate';
if ($eclate->getType() != 'folder') {
if ($eclate->get("o_className") == "Eclate") {
if ($eclate->isPublished()) {
$repository = $this->managerRegistry->getRepository(ActionLog::class);
$actionLog = $repository->findOneBy(['objectId' => $eclate->getId(), 'status' => "valide"], ['date' => 'desc']);
if (isset($actionLog) && $actionLog->getAction() != 'delete') {
$this->actionLogService->logActionEclate($o, 'delete', $type, "valide");
}
} else {
$this->actionLogService->logActionEclate($o, 'delete', 'eclate', "en-cours");
}
}
}
}
}
}