ImportRowModifierForm: cleanup

This commit is contained in:
Thomas Gelf 2018-07-13 11:46:25 +02:00
parent 089fbad29b
commit 85d748b295
1 changed files with 28 additions and 12 deletions

View File

@ -7,9 +7,9 @@ use Icinga\Application\Hook;
use Icinga\Exception\ConfigurationError; use Icinga\Exception\ConfigurationError;
use Icinga\Module\Director\Hook\ImportSourceHook; use Icinga\Module\Director\Hook\ImportSourceHook;
use Icinga\Module\Director\Hook\PropertyModifierHook; use Icinga\Module\Director\Hook\PropertyModifierHook;
use Icinga\Module\Director\Import\Import;
use Icinga\Module\Director\Objects\ImportSource; use Icinga\Module\Director\Objects\ImportSource;
use Icinga\Module\Director\Web\Form\DirectorObjectForm; use Icinga\Module\Director\Web\Form\DirectorObjectForm;
use RuntimeException;
class ImportRowModifierForm extends DirectorObjectForm class ImportRowModifierForm extends DirectorObjectForm
{ {
@ -19,15 +19,18 @@ class ImportRowModifierForm extends DirectorObjectForm
/** @var ImportSourceHook */ /** @var ImportSourceHook */
protected $importSource; protected $importSource;
/**
* @throws \Zend_Form_Exception
*/
public function setup() public function setup()
{ {
$this->addHidden('source_id', $this->source->id); $this->addHidden('source_id', $this->source->id);
$this->addElement('select', 'property_name', array( $this->addElement('select', 'property_name', [
'label' => $this->translate('Property'), 'label' => $this->translate('Property'),
'description' => $this->translate('This must be an import source column (property)'), 'description' => $this->translate('This must be an import source column (property)'),
'required' => true, 'required' => true,
)); ]);
try { try {
$sourceColumns = $this->enumSourceColumns(); $sourceColumns = $this->enumSourceColumns();
$this->getElement('property_name')->setOptions([ $this->getElement('property_name')->setOptions([
@ -37,7 +40,7 @@ class ImportRowModifierForm extends DirectorObjectForm
$this->getElement('property_name')->addError($e->getMessage()); $this->getElement('property_name')->addError($e->getMessage());
} }
$this->addElement('text', 'target_property', array( $this->addElement('text', 'target_property', [
'label' => $this->translate('Target property'), 'label' => $this->translate('Target property'),
'description' => $this->translate( 'description' => $this->translate(
'You might want to write the modified value to another (new) property.' 'You might want to write the modified value to another (new) property.'
@ -45,26 +48,26 @@ class ImportRowModifierForm extends DirectorObjectForm
. ' remain unmodified. Please leave this blank in case you just want to' . ' remain unmodified. Please leave this blank in case you just want to'
. ' modify the value of a specific property' . ' modify the value of a specific property'
), ),
)); ]);
$this->addElement('textarea', 'description', array( $this->addElement('textarea', 'description', [
'label' => $this->translate('Description'), 'label' => $this->translate('Description'),
'description' => $this->translate( 'description' => $this->translate(
'An extended description for this Import Row Modifier. This should explain' 'An extended description for this Import Row Modifier. This should explain'
. " it's purpose and why it has been put in place at all." . " it's purpose and why it has been put in place at all."
), ),
'rows' => '3', 'rows' => '3',
)); ]);
$error = false; $error = false;
try { try {
$mods = $this->enumModifiers(); $mods = $this->enumModifiers();
} catch (Exception $e) { } catch (Exception $e) {
$error = $e->getMessage(); $error = $e->getMessage();
$mods = $this->optionalEnum(array()); $mods = $this->optionalEnum([]);
} }
$this->addElement('select', 'provider_class', array( $this->addElement('select', 'provider_class', [
'label' => $this->translate('Modifier'), 'label' => $this->translate('Modifier'),
'required' => true, 'required' => true,
'description' => $this->translate( 'description' => $this->translate(
@ -72,7 +75,7 @@ class ImportRowModifierForm extends DirectorObjectForm
), ),
'multiOptions' => $this->optionalEnum($mods), 'multiOptions' => $this->optionalEnum($mods),
'class' => 'autosubmit', 'class' => 'autosubmit',
)); ]);
if ($error) { if ($error) {
$this->getElement('provider_class')->addError($error); $this->getElement('provider_class')->addError($error);
} }
@ -101,6 +104,10 @@ class ImportRowModifierForm extends DirectorObjectForm
$this->setButtons(); $this->setButtons();
} }
/**
* @return array
* @throws ConfigurationError
*/
protected function enumSourceColumns() protected function enumSourceColumns()
{ {
$columns = array_merge( $columns = array_merge(
@ -109,9 +116,14 @@ class ImportRowModifierForm extends DirectorObjectForm
); );
$columns = array_combine($columns, $columns); $columns = array_combine($columns, $columns);
return $columns; return $columns;
} }
/**
* @return ImportSourceHook
* @throws ConfigurationError
*/
protected function getImportSource() protected function getImportSource()
{ {
if ($this->importSource === null) { if ($this->importSource === null) {
@ -128,7 +140,7 @@ class ImportRowModifierForm extends DirectorObjectForm
{ {
/** @var PropertyModifierHook[] $hooks */ /** @var PropertyModifierHook[] $hooks */
$hooks = Hook::all('Director\\PropertyModifier'); $hooks = Hook::all('Director\\PropertyModifier');
$enum = array(); $enum = [];
foreach ($hooks as $hook) { foreach ($hooks as $hook) {
$enum[get_class($hook)] = $hook->getName(); $enum[get_class($hook)] = $hook->getName();
} }
@ -138,6 +150,9 @@ class ImportRowModifierForm extends DirectorObjectForm
return $enum; return $enum;
} }
/**
* @param null $class
*/
protected function addSettings($class = null) protected function addSettings($class = null)
{ {
if ($class === null) { if ($class === null) {
@ -146,7 +161,7 @@ class ImportRowModifierForm extends DirectorObjectForm
if ($class !== null) { if ($class !== null) {
if (! class_exists($class)) { if (! class_exists($class)) {
throw new ConfigurationError( throw new RuntimeException(
'The hooked class "%s" for this property modifier does no longer exist', 'The hooked class "%s" for this property modifier does no longer exist',
$class $class
); );
@ -159,6 +174,7 @@ class ImportRowModifierForm extends DirectorObjectForm
public function setSource(ImportSource $source) public function setSource(ImportSource $source)
{ {
$this->source = $source; $this->source = $source;
return $this; return $this;
} }
} }