<?php
/**
* Pimcore
*
* This source file is available under following license:
* - Pimcore Commercial License (PCL)
*
* @copyright Copyright (c) Pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license PCL
*/
namespace Pimcore\Bundle\DataHubFileExportBundle\EventSubscriber;
use Pimcore\Bundle\DataHubFileExportBundle\Event\Admin\ResetConfigEvent;
use Pimcore\Bundle\DataHubFileExportBundle\Event\Admin\ValidateConfigEvent;
use Pimcore\Bundle\DataHubFileExportBundle\Helper;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
class AdminSubscriber implements EventSubscriberInterface
{
/**
* @return string[]
*/
public static function getSubscribedEvents()
{
return [
ValidateConfigEvent::class => 'isValidConfig',
ResetConfigEvent::class => 'resetConfig',
];
}
public function resetConfig(ResetConfigEvent $e)
{
$config = $e->getConfig();
$exporter = Helper::getExporterService($config);
$exporter->resetData();
}
/**
* @param array $config
*
* @return array
*/
protected function getConfigErrors($config)
{
$errors = [];
if ($config['general']['active']) {
if (!$config['schema']['classId']) {
$errors[] = 'Please define a Data Class in the Schema definition';
}
if (!$config['schema']['config']) {
$errors[] = 'Please define a schema for the export';
}
if ($deliveryDestination = ($config['deliveryDestination'] ?? null)) {
if (!$config['deliveryDestination']['filename']) {
$errors[] = 'Please define a filename in Delivery destination';
}
if ($deliveryDestination['transmitter'] == 'localDirectory') {
if (!$deliveryDestination['transmitter_localDirectory']['directory']) {
$errors[] = 'Please provide a "Directory" in "Delivery destination"';
}
}
if ($deliveryDestination['transmitter'] == 'sftp') {
$settings = $deliveryDestination['transmitter_sftp'];
foreach (['host', 'port', 'username'] as $key) {
if (!$settings[$key]) {
$errors[] = 'Please provide "'.$key.'" in the SFTP settings';
}
}
}
if ($deliveryDestination['transmitter'] == 'http') {
$settings = $deliveryDestination['transmitter_http'];
foreach (['method', 'url'] as $key) {
if (!$settings[$key]) {
$errors[] = 'Please provide "'.$key.'" in the HTTP settings';
}
}
}
}
}
return $errors;
}
/**
* @param ValidateConfigEvent $e
*
* @return array
*
* @throws \Exception
*/
public function isValidConfig(ValidateConfigEvent $e)
{
$config = $e->getConfigData();
$errors = $this->getConfigErrors($config);
if ($errors) {
throw new \Exception(implode("\n", $errors));
}
return $errors;
}
}