From 0f8c275ed19d95e841e7ec28ad698728be56fcce Mon Sep 17 00:00:00 2001 From: Alejandro Gallardo Escobar Date: Thu, 4 Apr 2019 19:25:27 +0200 Subject: [PATCH] Visual Console Refactor: added the status image to the group model Former-commit-id: 80da05ae95ed87d4c7323594ada9e70c62f27501 --- .../models/VisualConsole/Items/Group.php | 76 +++++++++++++++++-- 1 file changed, 71 insertions(+), 5 deletions(-) diff --git a/pandora_console/include/rest-api/models/VisualConsole/Items/Group.php b/pandora_console/include/rest-api/models/VisualConsole/Items/Group.php index 559f8865d5..3759256968 100644 --- a/pandora_console/include/rest-api/models/VisualConsole/Items/Group.php +++ b/pandora_console/include/rest-api/models/VisualConsole/Items/Group.php @@ -33,8 +33,9 @@ final class Group extends Item { $return = parent::decode($data); $return['type'] = GROUP_ITEM; - $return['imageSrc'] = $this->extractImageSrc($data); - $return['groupId'] = $this->extractGroupId($data); + $return['imageSrc'] = static::extractImageSrc($data); + $return['groupId'] = static::extractGroupId($data); + $return['statusImageSrc'] = static::extractStatusImageSrc($data); return $return; } @@ -48,14 +49,14 @@ final class Group extends Item * * @throws \InvalidArgumentException When a valid image src can't be found. */ - private function extractImageSrc(array $data): string + private static function extractImageSrc(array $data): string { $imageSrc = static::notEmptyStringOr( static::issetInArray($data, ['imageSrc', 'image']), null ); - if ($imageSrc === null || \strlen($imageSrc) === 0) { + if ($imageSrc === null) { throw new \InvalidArgumentException( 'the image src property is required and should be a non empty string' ); @@ -65,6 +66,34 @@ final class Group extends Item } + /** + * Extract a status image src value. + * + * @param array $data Unknown input data structure. + * + * @return mixed String representing the status image url (not empty) + * or null. + * + * @throws \InvalidArgumentException When a valid status image src + * can't be found. + */ + private static function extractStatusImageSrc(array $data): string + { + $statusImageSrc = static::notEmptyStringOr( + static::issetInArray($data, ['statusImageSrc']), + null + ); + + if ($statusImageSrc === null) { + throw new \InvalidArgumentException( + 'the status image src property is required and should be a non empty string' + ); + } + + return $statusImageSrc; + } + + /** * Extract a group Id value. * @@ -74,7 +103,7 @@ final class Group extends Item * * @throws \InvalidArgumentException When a valid group Id can't be found. */ - private function extractGroupId(array $data): int + private static function extractGroupId(array $data): int { $groupId = static::parseIntOr( static::issetInArray($data, ['groupId', 'id_group']), @@ -91,4 +120,41 @@ final class Group extends Item } + /** + * Fetch a vc item data structure from the database using a filter. + * + * @param array $filter Filter of the Visual Console Item. + * + * @return array The Visual Console Item data structure stored into the DB. + * @throws \InvalidArgumentException When an agent Id cannot be found. + * + * @override Item::fetchDataFromDB. + */ + protected static function fetchDataFromDB(array $filter): array + { + // Due to this DB call, this function cannot be unit tested without + // a proper mock. + $data = parent::fetchDataFromDB($filter); + + /* + * Retrieve extra data. + */ + + // Load side libraries. + global $config; + include_once $config['homedir'].'/include/functions_groups.php'; + include_once $config['homedir'].'/include/functions_visual_map.php'; + + // Get the status img src. + $groupId = static::extractGroupId($data); + $status = \groups_get_status($groupId); + $data['statusImageSrc'] = \visual_map_get_image_status_element( + $data, + $status + ); + + return $data; + } + + }