Visual Console Refactor: refactored the code which fetch the items

Former-commit-id: 509310021f5a2e9f660e06f98aeba426f7e81b56
This commit is contained in:
Alejandro Gallardo Escobar 2019-04-08 10:30:45 +02:00
parent c91a4f8e4e
commit 6e9aad0634
3 changed files with 105 additions and 99 deletions

View File

@ -194,126 +194,107 @@ final class Container extends Model
/** /**
* Obtain a container data structure from the database using layout id * Obtain a item's class.
* and returns a valid representation of the model
* *
* @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]); switch ($type) {
if (!empty($layout_items) === true) { case STATIC_GRAPH:
$array_items = []; return Items\StaticGraph::class;
foreach ($layout_items as $key => $value) {
switch ($value['type']) {
case STATIC_GRAPH:
// code...
break;
case MODULE_GRAPH: case MODULE_GRAPH:
// code... return Items\ModuleGraph::class;
break;
case SIMPLE_VALUE: case SIMPLE_VALUE:
case SIMPLE_VALUE_MAX: case SIMPLE_VALUE_MAX:
case SIMPLE_VALUE_MIN: case SIMPLE_VALUE_MIN:
case SIMPLE_VALUE_AVG: case SIMPLE_VALUE_AVG:
array_push( return Items\SimpleValue::class;
$array_items,
(string) Items\SimpleValue::fromArray($value)
);
break;
case PERCENTILE_BAR: case PERCENTILE_BAR:
// code... case PERCENTILE_BUBBLE:
break; case CIRCULAR_PROGRESS_BAR:
case CIRCULAR_INTERIOR_PROGRESS_BAR:
return Items\Percentile::class;
case LABEL: case LABEL:
array_push( return Items\Label::class;
$array_items,
(string) Items\Label::fromArray($value)
);
break;
case ICON: case ICON:
array_push( return Items\Icon::class;
$array_items,
(string) Items\Icon::fromArray($value)
);
break;
case PERCENTILE_BUBBLE: case SERVICE:
// code... // TODO: Instance return.
break; break;
case SERVICE: case GROUP_ITEM:
// code... return Items\Group::class;
break;
case GROUP_ITEM: case BOX_ITEM:
array_push( return Items\Box::class;
$array_items,
(string) Items\Group::fromArray($value)
);
break;
case BOX_ITEM: case LINE_ITEM:
array_push( return Items\Line::class;
$array_items,
(string) Items\Box::fromArray($value)
);
break;
case LINE_ITEM: case AUTO_SLA_GRAPH:
array_push( return Items\EventsHistory::class;
$array_items,
(string) Items\Line::fromArray($value)
);
break;
case AUTO_SLA_GRAPH: case DONUT_GRAPH:
array_push( // TODO: Instance return.
$array_items, break;
(string) Items\EventsHistory::fromArray($value)
);
break;
case CIRCULAR_PROGRESS_BAR: case BARS_GRAPH:
// code... // TODO: Instance return.
break; break;
case CIRCULAR_INTERIOR_PROGRESS_BAR: case CLOCK:
// code... return Items\Clock::class;
break;
case DONUT_GRAPH: case COLOR_CLOUD:
// code... return Items\ColorCloud::class;
break;
case BARS_GRAPH: default:
// code... return Item::class;
break; }
}
case CLOCK:
array_push(
$array_items,
(string) Items\Clock::fromArray($value)
);
break;
case COLOR_CLOUD: /**
array_push( * Obtain a list of items which belong to the Visual Console.
$array_items, *
(string) Items\ColorCloud::fromArray($value) * @param integer $layoutId Identifier of the Visual Console.
); *
break; * @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;
} }

View File

@ -757,10 +757,10 @@ class Item extends Model
* *
* @param integer $id Identifier of the Visual Console Item. * @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. * @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]); return static::fromDB(['id' => $id]);
} }

View File

@ -4,6 +4,7 @@ declare(strict_types=1);
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Models\VisualConsole\Container as VisualConsole; use Models\VisualConsole\Container as VisualConsole;
use Models\VisualConsole\Items\StaticGraph;
/** /**
* Test for the Visual Console Container. * 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
);
}
} }