src/EventListener/AssetListener.php line 61

Open in your IDE?
  1. <?php
  2. namespace App\EventListener;
  3. use App\Entity\ActionLog;
  4. use App\Manager\AssetManager;
  5. use Pimcore\Model\Property\Predefined;
  6. use Psr\Log\LoggerInterface;
  7. use Pimcore\Event\Model\AssetEvent;
  8. use Pimcore\Model\Asset;
  9. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  10. use Symfony\Contracts\HttpClient\HttpClientInterface;
  11. use App\Service\ActionLogService;
  12. use App\Repository\ActionLogRepository;
  13. use Doctrine\Persistence\ManagerRegistry;
  14. class AssetListener
  15. {
  16.     private HttpClientInterface $client;
  17.     private AssetManager $assetManager;
  18.     private LoggerInterface $logger;
  19.     private $shercoNetworkSync;
  20.     private ActionLogService $actionLogService;
  21.     private $managerRegistry;
  22.     public function __construct($shercoNetworkSyncHttpClientInterface $clientAssetManager $assetManager,
  23.                                 LoggerInterface  $loggerActionLogService $actionLogServiceManagerRegistry $managerRegistry)
  24.     {
  25.         $this->shercoNetworkSync $shercoNetworkSync;
  26.         $this->client $client;
  27.         $this->assetManager =$assetManager;
  28.         $this->logger $logger;
  29.         $this->actionLogService $actionLogService;
  30.         $this->managerRegistry $managerRegistry;
  31.     }
  32.     public function onAssetPostAdd(AssetEvent $e)
  33.     {
  34.         $asset $e->getAsset();
  35.         if ($this->assetManager->assetIsCommunication($asset)) {
  36.             $type 'communication';
  37.         } elseif ($this->assetManager->assetIsInformationsTechniques($asset)) {
  38.             $type 'informationsTechniques';
  39.         } elseif ($this->assetManager->assetIsPCA($asset)) {
  40.             $type 'PCA';
  41.         } else {
  42.             $type 'null';
  43.         }
  44.         if ($asset->getType() != 'folder') {
  45.             $this->actionLogService->logActionAsset($e'add'$type);
  46.         }
  47.     }
  48.     public function onAssetPreUpdate(AssetEvent $e)
  49.     {
  50.         $asset $e->getAsset();
  51.         $repository $this->managerRegistry->getRepository(ActionLog::class);
  52.         $actionLog $repository->findOneBy(['objectId' => $asset->getId()], ['date' => 'desc']);
  53.         if ($this->assetManager->assetIsCommunication($asset)) {
  54.             $type 'communication';
  55.         } elseif ($this->assetManager->assetIsInformationsTechniques($asset)) {
  56.             $type 'informationsTechniques';
  57.         } elseif ($this->assetManager->assetIsPCA($asset)) {
  58.             $type 'PCA';
  59.         } else {
  60.             $type 'null';
  61.         }
  62.         if ($asset->getType() != 'folder') {
  63.             if (!$actionLog) {
  64.                 $this->actionLogService->logActionAsset($e'update'$type);
  65.             }
  66.         }
  67.     }
  68.     public function onAssetPostUpdate(AssetEvent $e)
  69.     {
  70.         $asset $e->getAsset();
  71.         $repository $this->managerRegistry->getRepository(ActionLog::class);
  72.         $actionLog $repository->findOneBy(['objectId' => $asset->getId()], ['date' => 'desc']);
  73.         if ($this->assetManager->assetIsCommunication($asset)) {
  74.             $type 'communication';
  75.         } elseif ($this->assetManager->assetIsInformationsTechniques($asset)) {
  76.             $type 'informationsTechniques';
  77.         } elseif ($this->assetManager->assetIsPCA($asset)) {
  78.             $type 'PCA';
  79.         } else {
  80.             $type 'null';
  81.         }
  82.         if ($asset->getType() != 'folder') {
  83.             if ($actionLog->getType() == $type) {
  84.                 $this->actionLogService->logActionAsset($e'update'$type);
  85.             } else {
  86.                 $this->actionLogService->logActionAsset($e'delete'$actionLog->getType());
  87.                 $this->actionLogService->logActionAsset($e'add'$type);
  88.             }
  89.         }
  90.     }
  91.     public function onAssetPostDelete(AssetEvent $e)
  92.     {
  93.         $asset $e->getAsset();
  94.         if ($this->assetManager->assetIsCommunication($asset)) {
  95.             $type 'communication';
  96.         } elseif ($this->assetManager->assetIsInformationsTechniques($asset)) {
  97.             $type 'informationsTechniques';
  98.         } elseif ($this->assetManager->assetIsPCA($asset)) {
  99.             $type 'PCA';
  100.         } else {
  101.             $type 'null';
  102.         }
  103.         if ($asset->getType() != 'folder') {
  104.             $this->actionLogService->logActionAsset($e'delete'$type);
  105.         }
  106.     }
  107. }