<?php
namespace App\EventListener;
use App\Entity\ActionLog;
use App\Manager\AssetManager;
use Pimcore\Model\Property\Predefined;
use Psr\Log\LoggerInterface;
use Pimcore\Event\Model\AssetEvent;
use Pimcore\Model\Asset;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use App\Service\ActionLogService;
use App\Repository\ActionLogRepository;
use Doctrine\Persistence\ManagerRegistry;
class AssetListener
{
private HttpClientInterface $client;
private AssetManager $assetManager;
private LoggerInterface $logger;
private $shercoNetworkSync;
private ActionLogService $actionLogService;
private $managerRegistry;
public function __construct($shercoNetworkSync, HttpClientInterface $client, AssetManager $assetManager,
LoggerInterface $logger, ActionLogService $actionLogService, ManagerRegistry $managerRegistry)
{
$this->shercoNetworkSync = $shercoNetworkSync;
$this->client = $client;
$this->assetManager =$assetManager;
$this->logger = $logger;
$this->actionLogService = $actionLogService;
$this->managerRegistry = $managerRegistry;
}
public function onAssetPostAdd(AssetEvent $e)
{
$asset = $e->getAsset();
if ($this->assetManager->assetIsCommunication($asset)) {
$type = 'communication';
} elseif ($this->assetManager->assetIsInformationsTechniques($asset)) {
$type = 'informationsTechniques';
} elseif ($this->assetManager->assetIsPCA($asset)) {
$type = 'PCA';
} else {
$type = 'null';
}
if ($asset->getType() != 'folder') {
$this->actionLogService->logActionAsset($e, 'add', $type);
}
}
public function onAssetPreUpdate(AssetEvent $e)
{
$asset = $e->getAsset();
$repository = $this->managerRegistry->getRepository(ActionLog::class);
$actionLog = $repository->findOneBy(['objectId' => $asset->getId()], ['date' => 'desc']);
if ($this->assetManager->assetIsCommunication($asset)) {
$type = 'communication';
} elseif ($this->assetManager->assetIsInformationsTechniques($asset)) {
$type = 'informationsTechniques';
} elseif ($this->assetManager->assetIsPCA($asset)) {
$type = 'PCA';
} else {
$type = 'null';
}
if ($asset->getType() != 'folder') {
if (!$actionLog) {
$this->actionLogService->logActionAsset($e, 'update', $type);
}
}
}
public function onAssetPostUpdate(AssetEvent $e)
{
$asset = $e->getAsset();
$repository = $this->managerRegistry->getRepository(ActionLog::class);
$actionLog = $repository->findOneBy(['objectId' => $asset->getId()], ['date' => 'desc']);
if ($this->assetManager->assetIsCommunication($asset)) {
$type = 'communication';
} elseif ($this->assetManager->assetIsInformationsTechniques($asset)) {
$type = 'informationsTechniques';
} elseif ($this->assetManager->assetIsPCA($asset)) {
$type = 'PCA';
} else {
$type = 'null';
}
if ($asset->getType() != 'folder') {
if ($actionLog->getType() == $type) {
$this->actionLogService->logActionAsset($e, 'update', $type);
} else {
$this->actionLogService->logActionAsset($e, 'delete', $actionLog->getType());
$this->actionLogService->logActionAsset($e, 'add', $type);
}
}
}
public function onAssetPostDelete(AssetEvent $e)
{
$asset = $e->getAsset();
if ($this->assetManager->assetIsCommunication($asset)) {
$type = 'communication';
} elseif ($this->assetManager->assetIsInformationsTechniques($asset)) {
$type = 'informationsTechniques';
} elseif ($this->assetManager->assetIsPCA($asset)) {
$type = 'PCA';
} else {
$type = 'null';
}
if ($asset->getType() != 'folder') {
$this->actionLogService->logActionAsset($e, 'delete', $type);
}
}
}