<?php
/**
* Pimcore
*
* This source file is available under two different licenses:
* - GNU General Public License version 3 (GPLv3)
* - Pimcore Commercial License (PCL)
* Full copyright and license information is available in
* LICENSE.md which is distributed with this source code.
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GPLv3 and PCL
*/
namespace App\mvk\Import\EventListener;
use Pimcore\Bundle\DataImporterBundle\Event\PostPreparationEvent;
use App\mvk\Import\Messenger\ImportHighPrioHandler;
use App\mvk\Import\Messenger\ImportLowPrioHandler;
use Pimcore\Log\ApplicationLogger;
use Pimcore\Bundle\DataImporterBundle\PimcoreDataImporterBundle;
use Pimcore\Bundle\DataImporterBundle\Processing\ImportProcessingService;
class DataImporterListener
{
public function __construct(
protected ImportHighPrioHandler $importHighPrioHandler,
protected ImportLowPrioHandler $importLowPrioHandler,
protected ApplicationLogger $applicationLogger,
protected ImportProcessingService $importProcessingService
) {
$this->applicationLogger = $applicationLogger;
$this->importProcessingService = $importProcessingService;
}
public function importPrepared(PostPreparationEvent $event)
{
$configName = $event->getConfigName();
// add Log Entry with some information about count of changes
$importStatus = $this->importProcessingService->getImportStatus($configName);
if (isset($importStatus['isRunning'])) {
if($importStatus['isRunning'] === false) {
$importStatus['isRunning'] = 'NO';
} else {
$importStatus['isRunning'] = 'YES';
}
}
$message = 'Import Status '.$configName.': '.$this->mapped_implode(', ', $importStatus, ' is ');
$this->applicationLogger->info($message, [
'component' => PimcoreDataImporterBundle::LOGGER_COMPONENT_PREFIX . $configName,
]);
if (str_contains($configName, 'Relation_') == true) {
// if the current config is a relation config use low prio messenger transport. Otherwise always use high transport message
$this->importLowPrioHandler->dispatchMessages($event->getExecutionType());
} else {
$this->importHighPrioHandler->dispatchMessages($event->getExecutionType());
}
}
/**
* helper function to implode the key value pairs from import status return array to a string message for application logger
*/
function mapped_implode($glue, $array, $symbol = '=') {
return implode($glue, array_map(
function($k, $v) use($symbol) {
return $k . $symbol . $v;
},
array_keys($array),
array_values($array)
)
);
}
}