vendor/pimcore/data-hub-file-export/src/EventSubscriber/AdminSubscriber.php line 33

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under following license:
  6.  * - Pimcore Commercial License (PCL)
  7.  *
  8.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  9.  *  @license    http://www.pimcore.org/license     PCL
  10.  */
  11. namespace Pimcore\Bundle\DataHubFileExportBundle\EventSubscriber;
  12. use Pimcore\Bundle\DataHubFileExportBundle\Event\Admin\ResetConfigEvent;
  13. use Pimcore\Bundle\DataHubFileExportBundle\Event\Admin\ValidateConfigEvent;
  14. use Pimcore\Bundle\DataHubFileExportBundle\Helper;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. class AdminSubscriber implements EventSubscriberInterface
  17. {
  18.     /**
  19.      * @return string[]
  20.      */
  21.     public static function getSubscribedEvents()
  22.     {
  23.         return [
  24.             ValidateConfigEvent::class => 'isValidConfig',
  25.             ResetConfigEvent::class => 'resetConfig',
  26.         ];
  27.     }
  28.     public function resetConfig(ResetConfigEvent $e)
  29.     {
  30.         $config $e->getConfig();
  31.         $exporter Helper::getExporterService($config);
  32.         $exporter->resetData();
  33.     }
  34.     /**
  35.      * @param array $config
  36.      *
  37.      * @return array
  38.      */
  39.     protected function getConfigErrors($config)
  40.     {
  41.         $errors = [];
  42.         if ($config['general']['active']) {
  43.             if (!$config['schema']['classId']) {
  44.                 $errors[] = 'Please define a Data Class in the Schema definition';
  45.             }
  46.             if (!$config['schema']['config']) {
  47.                 $errors[] = 'Please define a schema for the export';
  48.             }
  49.             if ($deliveryDestination = ($config['deliveryDestination'] ?? null)) {
  50.                 if (!$config['deliveryDestination']['filename']) {
  51.                     $errors[] = 'Please define a filename in Delivery destination';
  52.                 }
  53.                 if ($deliveryDestination['transmitter'] == 'localDirectory') {
  54.                     if (!$deliveryDestination['transmitter_localDirectory']['directory']) {
  55.                         $errors[] = 'Please provide a "Directory" in "Delivery destination"';
  56.                     }
  57.                 }
  58.                 if ($deliveryDestination['transmitter'] == 'sftp') {
  59.                     $settings $deliveryDestination['transmitter_sftp'];
  60.                     foreach (['host''port''username'] as $key) {
  61.                         if (!$settings[$key]) {
  62.                             $errors[] = 'Please provide "'.$key.'" in the SFTP settings';
  63.                         }
  64.                     }
  65.                 }
  66.                 if ($deliveryDestination['transmitter'] == 'http') {
  67.                     $settings $deliveryDestination['transmitter_http'];
  68.                     foreach (['method''url'] as $key) {
  69.                         if (!$settings[$key]) {
  70.                             $errors[] = 'Please provide "'.$key.'" in the HTTP settings';
  71.                         }
  72.                     }
  73.                 }
  74.             }
  75.         }
  76.         return $errors;
  77.     }
  78.     /**
  79.      * @param ValidateConfigEvent $e
  80.      *
  81.      * @return array
  82.      *
  83.      * @throws \Exception
  84.      */
  85.     public function isValidConfig(ValidateConfigEvent $e)
  86.     {
  87.         $config $e->getConfigData();
  88.         $errors $this->getConfigErrors($config);
  89.         if ($errors) {
  90.             throw new \Exception(implode("\n"$errors));
  91.         }
  92.         return $errors;
  93.     }
  94. }