Skip to content

Commit

Permalink
implement _sendResponse function
Browse files Browse the repository at this point in the history
  • Loading branch information
mudhoney committed May 17, 2024
1 parent 4cc6a01 commit 718a389
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 36 deletions.
60 changes: 25 additions & 35 deletions src/Module/WebClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -655,23 +655,10 @@ public function saveWebClientState() {

$state_key = $client_state->upsert($this->_params['state']);

$this->_printJSON(json_encode([
"status_code" => 200,
"status_txt" => "OK",
"data" => [
"state_id" => $state_key,
]
]));
return $this->_sendResponse(200, 'OK', $state_key);

} catch (\Exception $e) {

http_response_code(500);

$this->_printJSON(json_encode([
"status_code" => 500,
"status_txt" => "Server Error",
"data" => "",
]));
return $this->_sendResponse(500, 'Server Error', '');
}


Expand All @@ -691,32 +678,15 @@ public function getWebClientState()
$state = $client_state->find($this->_params['state_id']);

if(is_null($state)) {
http_response_code(404);
$this->_printJSON(json_encode([
"status_code" => 404,
"status_txt" => "Not Found",
"data" => "",
]));
return;
return $this->_sendResponse(404, 'Not Found', '');
}

$this->_printJSON(json_encode([
"status_code" => 200,
"status_txt" => "OK",
"data" => [
"state" => $state,
]
]));
return $this->_sendResponse(200, 'OK', $state);

} catch (\Exception $e) {

http_response_code(500);
return $this->_sendResponse(500, 'Server Error', '');

$this->_printJSON(json_encode([
"status_code" => 500,
"status_txt" => "Server Error",
"data" => "",
]));
}


Expand Down Expand Up @@ -1422,6 +1392,26 @@ private function _getTileCacheFilename($directory, $filename, $scale, $x, $y, $d
);
}

/**
* Helper function to handle response code and response message with
* output result as either JSON or JSONP
*
* @param int $code HTTP response code to return
* @param string $message Message for the response code,
* @param mixed $data Data can be anything
*
* @return void
*/
private function _sendResponse(int $code, string $message, mixed $data) : void
{
http_response_code($code);
$this->_printJSON(json_encode([
'status_code' => $code,
'status_txt' => $message,
'data' => $data,
]));
}

/**
* Helper function to output result as either JSON or JSONP
*
Expand Down
2 changes: 1 addition & 1 deletion tests/unit_tests/database/ClientStateTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php declare(strict_types=1);
/**
* @author Daniel Garcia-Briseno <daniel.garciabriseno@nasa.gov>
* @author Kasim Necdet Percinel <kasim.n.percinel@nasa.gov>
*/

use PHPUnit\Framework\TestCase;
Expand Down

0 comments on commit 718a389

Please sign in to comment.