323 lines
9.2 KiB
PHP
323 lines
9.2 KiB
PHP
<?php
|
|
|
|
class stTaskSchedulerImportConfiguration implements stTaskSchedulerImportConfigurationInterface
|
|
{
|
|
/**
|
|
* Aktualna wersja importu
|
|
*/
|
|
const VERSION = 2;
|
|
|
|
/**
|
|
* Domyślna konfiguracja importu
|
|
*/
|
|
const DEFAULTS = [
|
|
'curl_headers' => null,
|
|
'time_interval' => stTaskConfiguration::TIME_INTERVAL_1HOUR,
|
|
'execute_at' => '23:59:59',
|
|
'parent' => null,
|
|
'preloaded_data_import' => null,
|
|
'ignore_limit' => false,
|
|
'auth_type' => 'basic',
|
|
'version' => 1, // Wersja dla kompatybilności wstecz
|
|
'application' => 'backend',
|
|
'with' => [
|
|
'product_categories' => true,
|
|
'product_attributes' => true,
|
|
'product_images' => true,
|
|
'product_options' => true,
|
|
'product_description' => true,
|
|
'product_price' => true,
|
|
'product_price_array' => true,
|
|
'product_stock' => true,
|
|
'product_status' => false,
|
|
'url' => true,
|
|
'auth' => false,
|
|
],
|
|
'defaults' => [
|
|
'url' => '',
|
|
'code_prefix' => '',
|
|
'attributes' => 'default',
|
|
],
|
|
'custom_fields' => [],
|
|
];
|
|
|
|
protected $label;
|
|
protected $class;
|
|
protected $options;
|
|
protected $id;
|
|
protected $hashId;
|
|
|
|
protected static $configurations = [];
|
|
protected static $hashTable = null;
|
|
protected static $priority = 0;
|
|
|
|
/**
|
|
* Dodaje import
|
|
*
|
|
* @param string $id Id Importu
|
|
* @param string $className Klasa importu (klasa rozszerzająca stTaskSchedulerImportAbstract)
|
|
* @param string $label Nazwa importu
|
|
* @param array $options Dodatkowe opcje
|
|
* @return void
|
|
*/
|
|
public static function add(string $id, string $className, string $label, array $options = [])
|
|
{
|
|
if (strlen($id) > 64)
|
|
{
|
|
throw new stTaskSchedulerImportConfigurationException('The import id cannot exceed 64 characters');
|
|
}
|
|
|
|
if (!class_exists($className))
|
|
{
|
|
throw new stTaskSchedulerImportConfigurationException(sprintf('The class name "%s" does not exist', $className));
|
|
}
|
|
|
|
try
|
|
{
|
|
$options = stConfigHelper::mergeOptions($options, self::DEFAULTS);
|
|
}
|
|
catch (stConfigException $e)
|
|
{
|
|
throw new stTaskSchedulerImportConfigurationException($e->getMessage());
|
|
}
|
|
|
|
$imports = sfConfig::get('app_st_task_scheduler_import_imports', []);
|
|
|
|
if (isset($imports[$id]))
|
|
{
|
|
throw new stTaskSchedulerImportConfigurationException(sprintf('Import with id "%s" already exists', $id));
|
|
}
|
|
|
|
if (isset($options['parent']) && !isset($imports[$options['parent']]))
|
|
{
|
|
throw new stTaskSchedulerImportConfigurationException(sprintf('Parent import with id "%s" doesn\'t exist', $options['parent']));
|
|
}
|
|
|
|
self::mergeOptions($options, self::DEFAULTS);
|
|
|
|
$imports[$id] = [
|
|
'label' => $label,
|
|
'class' => $className,
|
|
'options' => $options,
|
|
];
|
|
|
|
sfConfig::set('app_st_task_scheduler_import_imports', $imports);
|
|
|
|
stTaskConfiguration::addTask($id, stTaskSchedulerImportTask::class, 'Import cykliczny - ' . $label, [
|
|
'time_interval' => $options['time_interval'],
|
|
'execute_at' => $options['execute_at'],
|
|
'priority' => self::$priority,
|
|
'is_system' => true,
|
|
'is_active' => false,
|
|
'parent' => isset($options['parent']) ? $options['parent'] : null,
|
|
]);
|
|
|
|
self::$priority++;
|
|
}
|
|
|
|
/**
|
|
* Zwraca instancje konfiguracji importu po jego id
|
|
*
|
|
* @param string $id
|
|
* @return stTaskSchedulerImportConfigurationInterface
|
|
*/
|
|
public static function get(string $id): stTaskSchedulerImportConfigurationInterface
|
|
{
|
|
if (!isset(self::$configurations[$id]))
|
|
{
|
|
$configurations = sfConfig::get('app_st_task_scheduler_import_imports', []);
|
|
|
|
if (!isset($configurations[$id]))
|
|
{
|
|
throw new stTaskSchedulerImportConfigurationException(sprintf('Import with id "%s" does not exist', $id));
|
|
}
|
|
|
|
self::$configurations[$id] = new static($id, $configurations[$id]['label'], $configurations[$id]['class'], $configurations[$id]['options']);
|
|
}
|
|
|
|
return self::$configurations[$id];
|
|
}
|
|
|
|
/**
|
|
* Zwraca instancje konfiguracji importu po jego hash_id
|
|
*
|
|
* @param string $hashId
|
|
* @return stTaskSchedulerImportConfigurationInterface
|
|
*/
|
|
public static function getByHashId(int $hashId): stTaskSchedulerImportConfigurationInterface
|
|
{
|
|
if (null === self::$hashTable)
|
|
{
|
|
$hashTable = [];
|
|
|
|
foreach (self::getAll() as $config)
|
|
{
|
|
$hashTable[$config->getHashId()] = $config->getId();
|
|
}
|
|
|
|
self::$hashTable = $hashTable;
|
|
}
|
|
|
|
if (!isset(self::$hashTable[$hashId]))
|
|
{
|
|
throw new stTaskSchedulerImportConfigurationException(sprintf('Import with hash_id "%s" does not exist', $hashId));
|
|
}
|
|
|
|
return self::get(self::$hashTable[$hashId]);
|
|
}
|
|
|
|
/**
|
|
* Zwraca listę konfiguracji importów
|
|
*
|
|
* @return stTaskSchedulerImportConfigurationInterface[]
|
|
*/
|
|
public static function getAll()
|
|
{
|
|
$ids = array_keys(sfConfig::get('app_st_task_scheduler_import_imports', []));
|
|
|
|
return array_map(function($id) {
|
|
return self::get($id);
|
|
}, $ids);
|
|
}
|
|
|
|
public function __construct(string $id, string $label, string $class, array $options = [])
|
|
{
|
|
$this->id = $id;
|
|
$this->hashId = sprintf('%u', crc32($id));
|
|
$this->label = $label;
|
|
$this->class = $class;
|
|
$this->options = $options;
|
|
}
|
|
|
|
public function getHashId(): int
|
|
{
|
|
return $this->hashId;
|
|
}
|
|
|
|
public function getId(): string
|
|
{
|
|
return $this->id;
|
|
}
|
|
|
|
public function getLabel(): string
|
|
{
|
|
return $this->label;
|
|
}
|
|
|
|
public function getClass(): string
|
|
{
|
|
return $this->class;
|
|
}
|
|
|
|
public function getParent(): ?stTaskSchedulerImportConfigurationInterface
|
|
{
|
|
return isset($this->options['parent']) ? self::get($this->options['parent']) : null;
|
|
}
|
|
|
|
public function getCustomFields(): array
|
|
{
|
|
return $this->getOption('custom_fields');
|
|
}
|
|
|
|
public function getChildren(): array
|
|
{
|
|
$children = [];
|
|
|
|
foreach (self::getAll() as $configuration)
|
|
{
|
|
if ($configuration->getParent() && $configuration->getParent()->getId() == $this->getId())
|
|
{
|
|
$children[] = $configuration;
|
|
}
|
|
}
|
|
|
|
return $children;
|
|
}
|
|
|
|
public function getOption(string $name)
|
|
{
|
|
if (null !== $this->getParent())
|
|
{
|
|
return $this->getParent()->getOption($name);
|
|
}
|
|
|
|
if (!isset($this->options[$name]) && !array_key_exists($name, $this->options))
|
|
{
|
|
$available = array_keys($this->options);
|
|
throw new stTaskSchedulerImportConfigurationException(sprintf('Option "%s" does not exist (available: "%s")', $name, implode('", "', $available)));
|
|
}
|
|
|
|
return $this->options[$name];
|
|
}
|
|
|
|
public function getUserOption(string $name, $default = null)
|
|
{
|
|
$config = stConfig::getInstance('stTaskSchedulerImportBackend');
|
|
|
|
$import = $config->get('import');
|
|
|
|
if (isset($import[$this->id][$name]))
|
|
{
|
|
return $import[$this->id][$name];
|
|
}
|
|
|
|
if (null !== $this->getParent())
|
|
{
|
|
return $this->getParent()->getUserOption($name, $default);
|
|
}
|
|
|
|
return $default;
|
|
}
|
|
|
|
public function with(string $name): bool
|
|
{
|
|
$with = $this->getOption('with');
|
|
|
|
if (!isset($with[$name]))
|
|
{
|
|
$available = array_keys($with);
|
|
throw new stTaskSchedulerImportConfigurationException(sprintf('Option "%s" does not exist in "with" parameter (available: "%s")', $name, implode('", "', $available)));
|
|
}
|
|
|
|
return $with[$name];
|
|
}
|
|
|
|
public function getDefault(string $name)
|
|
{
|
|
$defaults = $this->getOption('defaults');
|
|
|
|
if (!isset($defaults[$name]))
|
|
{
|
|
$available = array_keys($defaults);
|
|
throw new stTaskSchedulerImportConfigurationException(sprintf('Option "%s" does not exist in "defaults" parameter (available: "%s")', $name, implode('", "', $available)));
|
|
}
|
|
|
|
return $defaults[$name];
|
|
}
|
|
|
|
public function isActive(): bool
|
|
{
|
|
return $this->getUserOption('enabled', false);
|
|
}
|
|
|
|
public function getAuthentication(): string
|
|
{
|
|
return $this->getOption('auth_type');
|
|
}
|
|
|
|
protected static function mergeOptions(array &$options, array $defaults)
|
|
{
|
|
foreach ($defaults as $name => $value)
|
|
{
|
|
if (!isset($options[$name]))
|
|
{
|
|
$options[$name] = $value;
|
|
}
|
|
elseif (is_array($value))
|
|
{
|
|
self::mergeOptions($options[$name], $defaults[$name]);
|
|
}
|
|
}
|
|
}
|
|
}
|