From 6e9aad0634584064ced3a76ef12c84495def982c Mon Sep 17 00:00:00 2001 From: Alejandro Gallardo Escobar Date: Mon, 8 Apr 2019 10:30:45 +0200 Subject: [PATCH] Visual Console Refactor: refactored the code which fetch the items Former-commit-id: 509310021f5a2e9f660e06f98aeba426f7e81b56 --- .../models/VisualConsole/Container.php | 175 ++++++++---------- .../rest-api/models/VisualConsole/Item.php | 4 +- .../Models/VisualConsole/ContainerTest.php | 25 +++ 3 files changed, 105 insertions(+), 99 deletions(-) diff --git a/pandora_console/include/rest-api/models/VisualConsole/Container.php b/pandora_console/include/rest-api/models/VisualConsole/Container.php index b252e02846..9fda03c7e3 100644 --- a/pandora_console/include/rest-api/models/VisualConsole/Container.php +++ b/pandora_console/include/rest-api/models/VisualConsole/Container.php @@ -194,126 +194,107 @@ final class Container extends Model /** - * Obtain a container data structure from the database using layout id - * and returns a valid representation of the model + * Obtain a item's class. * - * @param integer $id_layout + * @param integer $type Type of the item of the Visual Console. * - * @return array + * @return mixed A reference to the item's class. */ - public static function getItemsFromDB(int $id_layout): string + public static function getItemClass(int $type) { - $layout_items = db_get_all_rows_filter('tlayout_data', ['id_layout' => $id_layout]); - if (!empty($layout_items) === true) { - $array_items = []; - foreach ($layout_items as $key => $value) { - switch ($value['type']) { - case STATIC_GRAPH: - // code... - break; + switch ($type) { + case STATIC_GRAPH: + return Items\StaticGraph::class; - case MODULE_GRAPH: - // code... - break; + case MODULE_GRAPH: + return Items\ModuleGraph::class; - case SIMPLE_VALUE: - case SIMPLE_VALUE_MAX: - case SIMPLE_VALUE_MIN: - case SIMPLE_VALUE_AVG: - array_push( - $array_items, - (string) Items\SimpleValue::fromArray($value) - ); - break; + case SIMPLE_VALUE: + case SIMPLE_VALUE_MAX: + case SIMPLE_VALUE_MIN: + case SIMPLE_VALUE_AVG: + return Items\SimpleValue::class; - case PERCENTILE_BAR: - // code... - break; + case PERCENTILE_BAR: + case PERCENTILE_BUBBLE: + case CIRCULAR_PROGRESS_BAR: + case CIRCULAR_INTERIOR_PROGRESS_BAR: + return Items\Percentile::class; - case LABEL: - array_push( - $array_items, - (string) Items\Label::fromArray($value) - ); - break; + case LABEL: + return Items\Label::class; - case ICON: - array_push( - $array_items, - (string) Items\Icon::fromArray($value) - ); - break; + case ICON: + return Items\Icon::class; - case PERCENTILE_BUBBLE: - // code... - break; + case SERVICE: + // TODO: Instance return. + break; - case SERVICE: - // code... - break; + case GROUP_ITEM: + return Items\Group::class; - case GROUP_ITEM: - array_push( - $array_items, - (string) Items\Group::fromArray($value) - ); - break; + case BOX_ITEM: + return Items\Box::class; - case BOX_ITEM: - array_push( - $array_items, - (string) Items\Box::fromArray($value) - ); - break; + case LINE_ITEM: + return Items\Line::class; - case LINE_ITEM: - array_push( - $array_items, - (string) Items\Line::fromArray($value) - ); - break; + case AUTO_SLA_GRAPH: + return Items\EventsHistory::class; - case AUTO_SLA_GRAPH: - array_push( - $array_items, - (string) Items\EventsHistory::fromArray($value) - ); - break; + case DONUT_GRAPH: + // TODO: Instance return. + break; - case CIRCULAR_PROGRESS_BAR: - // code... - break; + case BARS_GRAPH: + // TODO: Instance return. + break; - case CIRCULAR_INTERIOR_PROGRESS_BAR: - // code... - break; + case CLOCK: + return Items\Clock::class; - case DONUT_GRAPH: - // code... - break; + case COLOR_CLOUD: + return Items\ColorCloud::class; - case BARS_GRAPH: - // code... - break; + default: + return Item::class; + } + } - case CLOCK: - array_push( - $array_items, - (string) Items\Clock::fromArray($value) - ); - break; - case COLOR_CLOUD: - array_push( - $array_items, - (string) Items\ColorCloud::fromArray($value) - ); - break; - } - } + /** + * Obtain a list of items which belong to the Visual Console. + * + * @param integer $layoutId Identifier of the Visual Console. + * + * @return array A list of items. + * @throws \Exception When the data cannot be retrieved from the DB. + */ + public static function getItemsFromDB(int $layoutId): array + { + $filter = ['id_layout' => $layoutId]; + $fields = [ + 'id', + 'type', + ]; + $rows = \db_get_all_rows_filter('tlayout_data', $filter, $fields); + + if ($rows === false) { + $rows = []; + // TODO: throw new \Exception('error fetching the data from the DB'); } - return json_encode($array_items); + $items = []; + + foreach ($rows as $data) { + $itemId = (int) $data['id']; + $itemType = (int) $data['type']; + $class = static::getItemClass($itemType); + \array_push($items, $class::fromDBWithId($itemId)); + } + + return $items; } diff --git a/pandora_console/include/rest-api/models/VisualConsole/Item.php b/pandora_console/include/rest-api/models/VisualConsole/Item.php index b27e708998..27959a11dc 100644 --- a/pandora_console/include/rest-api/models/VisualConsole/Item.php +++ b/pandora_console/include/rest-api/models/VisualConsole/Item.php @@ -757,10 +757,10 @@ class Item extends Model * * @param integer $id Identifier of the Visual Console Item. * - * @return array The Visual Console Item data structure stored into the DB. + * @return mixed The Visual Console Item data structure stored into the DB. * @throws \Exception When the data cannot be retrieved from the DB. */ - public static function fromDBWithId(int $id): array + public static function fromDBWithId(int $id) { return static::fromDB(['id' => $id]); } diff --git a/pandora_console/tests/Functional/Models/VisualConsole/ContainerTest.php b/pandora_console/tests/Functional/Models/VisualConsole/ContainerTest.php index 146f833735..e03004d97c 100644 --- a/pandora_console/tests/Functional/Models/VisualConsole/ContainerTest.php +++ b/pandora_console/tests/Functional/Models/VisualConsole/ContainerTest.php @@ -4,6 +4,7 @@ declare(strict_types=1); use PHPUnit\Framework\TestCase; use Models\VisualConsole\Container as VisualConsole; +use Models\VisualConsole\Items\StaticGraph; /** * Test for the Visual Console Container. @@ -241,4 +242,28 @@ class ContainerTest extends TestCase } + /** + * Test if the item's instance is returned properly. + * + * @return void + */ + public function testItemClassIsReturned(): void + { + $this->assertEquals( + VisualConsole::getItemClass(STATIC_GRAPH), + Models\VisualConsole\Items\StaticGraph::class + ); + + $this->assertEquals( + VisualConsole::getItemClass(COLOR_CLOUD), + Models\VisualConsole\Items\ColorCloud::class + ); + + $this->assertEquals( + VisualConsole::getItemClass(LABEL), + Models\VisualConsole\Items\Label::class + ); + } + + }