Skip to content

Commit

Permalink
Any error that prevents a successful request generates an exception.
Browse files Browse the repository at this point in the history
  • Loading branch information
llagerlof committed Apr 14, 2021
1 parent 1bd7e73 commit 3fb24a0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
39 changes: 21 additions & 18 deletions MoodleRest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
* MoodleRest is a class to query Moodle REST webservices
*
* @package MoodleRest
* @version 2.3.0
* @version 2.4.0
* @author Lawrence Lagerlof <[email protected]>
* @copyright 2018 Lawrence Lagerlof
* @copyright 2021 Lawrence Lagerlof
* @link http://github.com/llagerlof/MoodleRest
* @license https://opensource.org/licenses/MIT MIT
*/
Expand Down Expand Up @@ -119,7 +119,7 @@ public function __construct($server_address = null, $token = null, $return_forma
$this->server_address = $server_address;
$this->token = $token;
if (!is_null($return_format) && $return_format <> 'json' && $return_format <> 'xml' && $return_format <> 'array') {
trigger_error("Invalid return format: '$return_format'", E_USER_WARNING);
throw new Exception("MoodleRest: Invalid return format: '$return_format'.");
}
$this->return_format = $return_format;
}
Expand Down Expand Up @@ -157,6 +157,7 @@ public function getServerAddress()
public function setToken($token)
{
$this->token = $token;

return $this;
}

Expand All @@ -180,9 +181,10 @@ public function getToken()
public function setReturnFormat($return_format) // json, xml, array
{
if ($return_format <> 'json' && $return_format <> 'xml' && $return_format <> 'array') {
trigger_error("Invalid return format: '$return_format'", E_USER_WARNING);
throw new Exception("MoodleRest: Invalid return format: '$return_format'.");
}
$this->return_format = $return_format;

return $this;
}

Expand Down Expand Up @@ -388,32 +390,23 @@ public function printRequest()
*/
public function request($function, $parameters = null, $method = self::METHOD_GET)
{
$fatal = 0;
if (empty($this->server_address)) {
trigger_error('Empty server address. Use setServerAddress() or put the address on constructor.', E_USER_WARNING);
$fatal = 1;
throw new Exception('MoodleRest: Empty server address. Use setServerAddress() or put the address on constructor.');
}
if (empty($this->token)) {
trigger_error('Empty token. Use setToken() or put the token on constructor. ', E_USER_WARNING);
$fatal = 1;
throw new Exception('MoodleRest: Empty token. Use setToken() or put the token on constructor.');
}
if (empty($this->return_format)) {
trigger_error('Empty return format. Use setReturnFormat()', E_USER_WARNING);
$fatal = 1;
throw new Exception('MoodleRest: Empty return format. Use setReturnFormat().');
}
if (empty($function)) {
trigger_error('Empty function. Fill the first parameter of request()', E_USER_WARNING);
$fatal = 1;
throw new Exception('MoodleRest: Empty function. Fill the first parameter of request().');
}
if (!is_null($parameters)) {
if (!is_array($parameters)) {
trigger_error('The second parameter of request() should be an array', E_USER_WARNING);
$fatal = 1;
throw new Exception('MoodleRest: The second parameter of request() should be an array.');
}
}
if ($fatal) {
trigger_error('Fix above errors and try again', E_USER_ERROR);
}

if ($this->getReturnFormat() == 'array' || $this->getReturnFormat() == 'json') {
$return_format = 'json';
Expand Down Expand Up @@ -442,6 +435,11 @@ public function request($function, $parameters = null, $method = self::METHOD_GE
if ($this->getMethod() != self::METHOD_POST) {
// GET
$moodle_request = file_get_contents($this->getUrl(false));

if ($moodle_request === false) {
throw new Exception('MoodleRest: Error trying to connect to Moodle server on GET request. Check PHP warning messages.');
}

$this->debug($this->getUrl(), $function, self::METHOD_GET, $moodle_request);
} else {
// POST
Expand All @@ -454,6 +452,11 @@ public function request($function, $parameters = null, $method = self::METHOD_GE
);
$context = stream_context_create($options);
$moodle_request = file_get_contents($post_url, false, $context);

if ($moodle_request === false) {
throw new Exception('MoodleRest: Error trying to connect to Moodle server on POST request. Check PHP warning messages.');
}

$this->debug($this->getUrl(), $function, self::METHOD_POST, $moodle_request);
}

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ If you need a detailed explanation about the **$parameters** format of the **req
```json
{
"require": {
"llagerlof/moodlerest": "2.3.0"
"llagerlof/moodlerest": "2.4.0"
}
}
```
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "llagerlof/moodlerest",
"description": "MoodleRest is a PHP class to query Moodle REST webservices",
"license": "MIT",
"version": "2.3.0",
"version": "2.4.0",
"homepage": "https://github.com/llagerlof",
"repository": {
"type": "git",
Expand Down

0 comments on commit 3fb24a0

Please sign in to comment.