From c781a551ae84127ef05eaa36909dca44e49e1200 Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Wed, 19 Apr 2017 23:43:24 +0200 Subject: [locationinfo] Better backend-specific property handling, get rid of URL - The backend URL still had special treatment for legacy reasons, when it would be perfectly fine to make it just another generic property the backend has to define. - Allow for the backend to declare a default value for properties. - Base class will now check and sanitize the setCredentials() input. --- .../locationinfo/inc/coursebackend.inc.php | 47 +++++++++++++++++----- 1 file changed, 37 insertions(+), 10 deletions(-) (limited to 'modules-available/locationinfo/inc/coursebackend.inc.php') diff --git a/modules-available/locationinfo/inc/coursebackend.inc.php b/modules-available/locationinfo/inc/coursebackend.inc.php index 7dc50549..0d84b0fb 100644 --- a/modules-available/locationinfo/inc/coursebackend.inc.php +++ b/modules-available/locationinfo/inc/coursebackend.inc.php @@ -22,10 +22,6 @@ abstract class CourseBackend * @var int as internal serverId */ protected $serverId; - /** - * @var string url of the service - */ - protected $location; /** * @const int max number of additional locations to fetch (for backends that benefit from request coalesc.) */ @@ -36,7 +32,6 @@ abstract class CourseBackend */ public final function __construct() { - $this->location = ""; $this->error = false; } @@ -97,7 +92,7 @@ abstract class CourseBackend /** - * @returns array with parameter name as key and and an array with type, help text and mask as value + * @returns \BackendProperty[] list of properties that need to be set */ public abstract function getCredentials(); @@ -110,12 +105,10 @@ abstract class CourseBackend * uses json to setCredentials, the json must follow the form given in * getCredentials * - * @param array $data with the credentials - * @param string $url address of the server - * @param int $serverId ID of the server + * @param array $data assoc array with data required by backend * @returns bool if the credentials were in the correct format */ - public abstract function setCredentials($data, $url, $serverId); + public abstract function setCredentialsInternal($data); /** * @return int desired caching time of results, in seconds. 0 = no caching @@ -215,6 +208,25 @@ abstract class CourseBackend return $returnValue; } + public final function setCredentials($serverId, $data) + { + foreach ($this->getCredentials() as $prop) { + if (!isset($data[$prop->property])) { + $data[$prop->property] = $prop->default; + } + if (in_array($prop->type, ['string', 'bool', 'int'])) { + settype($data[$prop->property], $prop->type); + } else { + settype($data[$prop->property], 'string'); + } + } + if ($this->setCredentialsInternal($data)) { + $this->serverId = $serverId; + return true; + } + return false; + } + /** * @return false if there was no error string with error message if there was one */ @@ -297,3 +309,18 @@ abstract class CourseBackend } } + +/** + * Class BackendProperty describes a property a backend requires to define its functionality + */ +class BackendProperty { + public $property; + public $type; + public $default; + public function __construct($property, $type, $default = '') + { + $this->property = $property; + $this->type = $type; + $this->default = $default; + } +} -- cgit v1.2.3-55-g7522