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
* 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']) {
switch ($type) {
case STATIC_GRAPH:
// code...
break;
return Items\StaticGraph::class;
case MODULE_GRAPH:
// code...
break;
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;
return Items\SimpleValue::class;
case PERCENTILE_BAR:
// code...
break;
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;
return Items\Label::class;
case ICON:
array_push(
$array_items,
(string) Items\Icon::fromArray($value)
);
break;
case PERCENTILE_BUBBLE:
// code...
break;
return Items\Icon::class;
case SERVICE:
// code...
// TODO: Instance return.
break;
case GROUP_ITEM:
array_push(
$array_items,
(string) Items\Group::fromArray($value)
);
break;
return Items\Group::class;
case BOX_ITEM:
array_push(
$array_items,
(string) Items\Box::fromArray($value)
);
break;
return Items\Box::class;
case LINE_ITEM:
array_push(
$array_items,
(string) Items\Line::fromArray($value)
);
break;
return Items\Line::class;
case AUTO_SLA_GRAPH:
array_push(
$array_items,
(string) Items\EventsHistory::fromArray($value)
);
break;
case CIRCULAR_PROGRESS_BAR:
// code...
break;
case CIRCULAR_INTERIOR_PROGRESS_BAR:
// code...
break;
return Items\EventsHistory::class;
case DONUT_GRAPH:
// code...
// TODO: Instance return.
break;
case BARS_GRAPH:
// code...
// TODO: Instance return.
break;
case CLOCK:
array_push(
$array_items,
(string) Items\Clock::fromArray($value)
);
break;
return Items\Clock::class;
case COLOR_CLOUD:
array_push(
$array_items,
(string) Items\ColorCloud::fromArray($value)
);
break;
}
return Items\ColorCloud::class;
default:
return Item::class;
}
}
return json_encode($array_items);
/**
* 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');
}
$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.
*
* @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]);
}

View File

@ -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
);
}
}