Visual Console Refactor: error fixes

Former-commit-id: 01d70e83426c5eb21b2184190d6c2a39111a1c75
This commit is contained in:
Alejandro Gallardo Escobar 2019-04-03 14:46:49 +02:00
parent 3afe6f8d3a
commit 3df4c08782
2 changed files with 32 additions and 36 deletions

View File

@ -6,7 +6,7 @@ namespace Models\VisualConsole\Items;
use Models\VisualConsole\Item;
/**
* Model of a Clock item of the Visual Console.
* Model of a clock item of the Visual Console.
*/
final class Clock extends Item
{
@ -26,6 +26,8 @@ final class Clock extends Item
* @param array $data Input data.
*
* @return array Data structure representing the model.
* @throws \InvalidArgumentException When there is a problem with
* the time management.
*
* @overrides Item::decode.
*/
@ -33,20 +35,25 @@ final class Clock extends Item
{
$clockData = parent::decode($data);
$clockData['type'] = CLOCK;
$clockData['clockType'] = $this->extractClockType($data);
$clockData['clockFormat'] = $this->extractClockFormat($data);
$clockData['clockTimezone'] = $this->extractClockTimezone($data);
$clockData['clockType'] = static::extractClockType($data);
$clockData['clockFormat'] = static::extractClockFormat($data);
$clockData['clockTimezone'] = static::extractClockTimezone($data);
try {
$timezone = new \DateTimeZone($clockData['clockTimezone']);
$timezoneUTC = new \DateTimeZone('UTC');
$clockData['clockTimezoneOffset'] = $timezone->getOffset(new \DateTime('now', $timezoneUTC));
} catch (Exception $e) {
$dateTimeUtc = new \DateTime('now', $timezoneUTC);
$clockData['clockTimezoneOffset'] = $timezone->getOffset(
$dateTimeUtc
);
} catch (\Exception $e) {
throw new \InvalidArgumentException($e->getMessage());
}
$clockData['showClockTimezone'] = $this->extractShowClockTimezone($data);
$clockData['color'] = $this->extractColor($data);
$clockData['showClockTimezone'] = static::parseBool(
$data['showClockTimezone']
);
$clockData['color'] = static::extractColor($data);
return $clockData;
}
@ -56,9 +63,9 @@ final class Clock extends Item
*
* @param array $data Unknown input data structure.
*
* @return string Digital or analogic. analogic by default.
* @return string One of 'digital' or 'analogic'. 'analogic' by default.
*/
private function extractClockType(array $data): string
private static function extractClockType(array $data): string
{
$clockType = static::notEmptyStringOr(
static::issetInArray($data, ['clockType', 'clock_animation']),
@ -81,9 +88,9 @@ final class Clock extends Item
*
* @param array $data Unknown input data structure.
*
* @return string Time or datetime. datetime by default.
* @return string One of 'time' or 'datetime'. 'datetime' by default.
*/
private function extractClockFormat(array $data): string
private static function extractClockFormat(array $data): string
{
$clockFormat = static::notEmptyStringOr(
static::issetInArray($data, ['clockFormat', 'time_format']),
@ -106,8 +113,10 @@ final class Clock extends Item
* @param array $data Unknown input data structure.
*
* @return string
* @throws \InvalidArgumentException When a valid clock timezone cannot be
* extracted.
*/
private function extractClockTimezone(array $data): string
private static function extractClockTimezone(array $data): string
{
$clockTimezone = static::notEmptyStringOr(
static::issetInArray($data, ['clockTimezone', 'timezone']),
@ -118,24 +127,9 @@ final class Clock extends Item
throw new \InvalidArgumentException(
'the clockTimezone property is required and should be string'
);
} else {
return $clockTimezone;
}
}
/**
* Extract a clock timezone value.
*
* @param array $data Unknown input data structure.
*
* @return boolean
*/
private function extractShowClockTimezone(array $data): bool
{
return static::parseBool(
static::issetInArray($data, ['showClockTimezone'])
);
return $clockTimezone;
}
@ -144,9 +138,9 @@ final class Clock extends Item
*
* @param array $data Unknown input data structure.
*
* @return mixed returns a color or null
* @return mixed returns a color or null.
*/
private function extractColor(array $data)
private static function extractColor(array $data)
{
return static::notEmptyStringOr(
static::issetInArray($data, ['color', 'fill_color']),

View File

@ -59,7 +59,7 @@ class StaticGrahpTest extends TestCase
public function testContainerIsRepresentedAsJson(): void
{
$this->assertEquals(
'{"aclGroupId":null,"agentId":null,"agentName":null,"height":0,"id":7,"imageSrc":"image.jpg","isLinkEnabled":true,"isOnTop":false,"label":null,"labelPosition":"up","linkedLayoutAgentId":null,"linkedLayoutId":null,"linkedLayoutStatusType":"default","moduleId":null,"moduleName":null,"parentId":null,"showLastValueTooltip":"default","type":0,"width":0,"x":-666,"y":76}',
'{"aclGroupId":null,"agentId":null,"agentName":null,"height":0,"id":7,"imageSrc":"image.jpg","isLinkEnabled":true,"isOnTop":false,"label":null,"labelPosition":"up","linkedLayoutAgentId":null,"linkedLayoutId":null,"linkedLayoutStatusType":"default","moduleId":null,"moduleName":null,"parentId":null,"showLastValueTooltip":"default","statusImageSrc":null,"type":0,"width":0,"x":-666,"y":76}',
(string) StaticGraph::fromArray(
[
'id' => 7,
@ -79,7 +79,7 @@ class StaticGrahpTest extends TestCase
);
$this->assertEquals(
'{"aclGroupId":null,"agentId":null,"agentName":null,"height":0,"id":7,"imageSrc":"image.jpg","isLinkEnabled":true,"isOnTop":false,"label":null,"labelPosition":"up","linkedLayoutAgentId":null,"linkedLayoutId":null,"linkedLayoutStatusType":"default","moduleId":null,"moduleName":null,"parentId":null,"showLastValueTooltip":"disabled","type":0,"width":0,"x":-666,"y":76}',
'{"aclGroupId":null,"agentId":null,"agentName":null,"height":0,"id":7,"imageSrc":"image.jpg","isLinkEnabled":true,"isOnTop":false,"label":null,"labelPosition":"up","linkedLayoutAgentId":null,"linkedLayoutId":null,"linkedLayoutStatusType":"default","moduleId":null,"moduleName":null,"parentId":null,"showLastValueTooltip":"disabled","statusImageSrc":null,"type":0,"width":0,"x":-666,"y":76}',
(string) StaticGraph::fromArray(
[
'id' => 7,
@ -100,7 +100,7 @@ class StaticGrahpTest extends TestCase
);
$this->assertEquals(
'{"aclGroupId":null,"agentId":null,"agentName":null,"height":0,"id":7,"imageSrc":"image.jpg","isLinkEnabled":true,"isOnTop":false,"label":null,"labelPosition":"up","linkedLayoutAgentId":3,"linkedLayoutId":2,"linkedLayoutStatusType":"default","metaconsoleId":5,"moduleId":null,"moduleName":null,"parentId":null,"showLastValueTooltip":"default","type":0,"width":0,"x":-666,"y":76}',
'{"aclGroupId":null,"agentId":null,"agentName":null,"height":0,"id":7,"imageSrc":"image.jpg","isLinkEnabled":true,"isOnTop":false,"label":null,"labelPosition":"up","linkedLayoutAgentId":3,"linkedLayoutId":2,"linkedLayoutStatusType":"default","metaconsoleId":5,"moduleId":null,"moduleName":null,"parentId":null,"showLastValueTooltip":"default","statusImageSrc":"image.bad.jpg","type":0,"width":0,"x":-666,"y":76}',
(string) StaticGraph::fromArray(
[
'id' => 7,
@ -118,12 +118,13 @@ class StaticGrahpTest extends TestCase
'id_metaconsole' => 5,
'linked_layout_node_id' => 3,
'linkedLayoutId' => 2,
'statusImageSrc' => 'image.bad.jpg',
]
)
);
$this->assertEquals(
'{"aclGroupId":null,"agentId":null,"agentName":null,"height":0,"id":7,"imageSrc":"image.jpg","isLinkEnabled":true,"isOnTop":false,"label":null,"labelPosition":"up","linkedLayoutAgentId":null,"linkedLayoutId":1,"linkedLayoutStatusType":"default","moduleId":null,"moduleName":null,"parentId":null,"showLastValueTooltip":"default","type":0,"width":0,"x":-666,"y":76}',
'{"aclGroupId":null,"agentId":null,"agentName":null,"height":0,"id":7,"imageSrc":"image.jpg","isLinkEnabled":true,"isOnTop":false,"label":null,"labelPosition":"up","linkedLayoutAgentId":null,"linkedLayoutId":1,"linkedLayoutStatusType":"default","moduleId":null,"moduleName":null,"parentId":null,"showLastValueTooltip":"default","statusImageSrc":null,"type":0,"width":0,"x":-666,"y":76}',
(string) StaticGraph::fromArray(
[
'id' => 7,
@ -144,7 +145,7 @@ class StaticGrahpTest extends TestCase
);
$this->assertEquals(
'{"aclGroupId":null,"agentId":null,"agentName":null,"height":0,"id":7,"imageSrc":"image.jpg","isLinkEnabled":true,"isOnTop":false,"label":null,"labelPosition":"up","linkedLayoutAgentId":null,"linkedLayoutId":2,"linkedLayoutStatusType":"service","linkedLayoutStatusTypeCriticalThreshold":80,"linkedLayoutStatusTypeWarningThreshold":50,"moduleId":null,"moduleName":null,"parentId":null,"showLastValueTooltip":"default","type":0,"width":0,"x":-666,"y":76}',
'{"aclGroupId":null,"agentId":null,"agentName":null,"height":0,"id":7,"imageSrc":"image.jpg","isLinkEnabled":true,"isOnTop":false,"label":null,"labelPosition":"up","linkedLayoutAgentId":null,"linkedLayoutId":2,"linkedLayoutStatusType":"service","linkedLayoutStatusTypeCriticalThreshold":80,"linkedLayoutStatusTypeWarningThreshold":50,"moduleId":null,"moduleName":null,"parentId":null,"showLastValueTooltip":"default","statusImageSrc":"image.bad.jpg","type":0,"width":0,"x":-666,"y":76}',
(string) StaticGraph::fromArray(
[
'id' => 7,
@ -163,6 +164,7 @@ class StaticGrahpTest extends TestCase
'linked_layout_status_type' => 'service',
'linkedLayoutStatusTypeWarningThreshold' => 50,
'linked_layout_status_as_service_critical' => 80,
'statusImageSrc' => 'image.bad.jpg',
]
)
);