Visual Console Refactor: added a system to abstract the html validation, extraction and encoding

Former-commit-id: d53b9fba38b1a57f2c00c627b30d6a3f2d8e64ef
This commit is contained in:
Alejandro Gallardo Escobar 2019-04-03 12:07:48 +02:00
parent ca4997fd7f
commit 24d8964fd9
2 changed files with 45 additions and 42 deletions

View File

@ -33,6 +33,13 @@ class Item extends Model
*/ */
protected static $useLinkedVisualConsole = false; protected static $useLinkedVisualConsole = false;
/**
* Used to decide wether to validate, extract and encode HTML output or not.
*
* @var boolean
*/
protected static $useHtmlOutput = false;
/** /**
* Validate the received data structure to ensure if we can extract the * Validate the received data structure to ensure if we can extract the
@ -148,6 +155,17 @@ class Item extends Model
} }
} }
} }
// The item uses HTML output.
if (static::$useHtmlOutput === true) {
if (static::notEmptyStringOr($data['encodedHtml'], null) === null
&& static::notEmptyStringOr($data['html'], null) === null
) {
throw new \InvalidArgumentException(
'the html property is required and should be a not empty string'
);
}
}
} }
@ -196,6 +214,10 @@ class Item extends Model
); );
} }
if (static::$useHtmlOutput === true) {
$decodedData['encodedHtml'] = static::extractEncodedHtml($data);
}
return $decodedData; return $decodedData;
} }
@ -569,6 +591,25 @@ class Item extends Model
} }
/**
* Extract a encoded HTML representation of the item.
*
* @param array $data Unknown input data structure.
*
* @return string The HTML representation in base64 encoding.
*/
private static function extractEncodedHtml(array $data): string
{
if (isset($data['encodedHtml']) === true) {
return $data['encodedHtml'];
} else if (isset($data['html']) === true) {
return \base64_encode($data['html']);
}
return '';
}
/** /**
* Fetch a vc item data structure from the database using a filter. * Fetch a vc item data structure from the database using a filter.
* *

View File

@ -27,32 +27,12 @@ final class EventsHistory extends Item
*/ */
protected static $useLinkedVisualConsole = true; protected static $useLinkedVisualConsole = true;
/** /**
* Validate the received data structure to ensure if we can extract the * Used to enable validation, extraction and encodeing of the HTML output.
* values required to build the model.
* *
* @param array $data Input data. * @var boolean
*
* @return void
*
* @throws \InvalidArgumentException If any input value is considered
* invalid.
*
* @overrides Item::validateData.
*/ */
protected function validateData(array $data): void protected static $useHtmlOutput = true;
{
parent::validateData($data);
if (static::notEmptyStringOr($data['encodedHtml'], null) === null
&& static::notEmptyStringOr($data['html'], null) === null
) {
throw new \InvalidArgumentException(
'the html property is required and should be string'
);
}
}
/** /**
@ -69,7 +49,6 @@ final class EventsHistory extends Item
$return = parent::decode($data); $return = parent::decode($data);
$return['type'] = AUTO_SLA_GRAPH; $return['type'] = AUTO_SLA_GRAPH;
$return['maxTime'] = $this->extractMaxTime($data); $return['maxTime'] = $this->extractMaxTime($data);
$return['encodedHtml'] = $this->extractEncodedHtml($data);
return $return; return $return;
} }
@ -90,23 +69,6 @@ final class EventsHistory extends Item
} }
/**
* Extract a encoded HTML representation of the item.
*
* @param array $data Unknown input data structure.
*
* @return string The HTML representation in base64 encoding.
*/
private static function extractEncodedHtml(array $data): string
{
if (isset($data['encodedHtml']) === true) {
return $data['encodedHtml'];
} else if (isset($data['html']) === true) {
return \base64_encode($data['html']);
}
}
/** /**
* Fetch a vc item data structure from the database using a filter. * Fetch a vc item data structure from the database using a filter.
* *
@ -153,7 +115,7 @@ final class EventsHistory extends Item
); );
$html .= '</div>'; $html .= '</div>';
$data['encodedHtml'] = \base64_encode($html); $data['html'] = $html;
return $data; return $data;
} }