Page MenuHomeDevCentral

No OneTemporary

diff --git a/src/BaseService.php b/src/BaseService.php
index dcd0d65..6ff08a6 100644
--- a/src/BaseService.php
+++ b/src/BaseService.php
@@ -1,35 +1,45 @@
<?php
namespace Nasqueron\Api\ServersLog;
use Exception;
class BaseService {
///
/// Helper methods
///
protected function getBodyObject () {
$content= file_get_contents('php://input');
switch ($_SERVER["HTTP_CONTENT_TYPE"]) {
case "application/json":
return json_decode($content);
default:
throw new Exception("Unknown content type.");
}
}
protected function sendSuccessResponse ($responseBody = '') {
// HTTP 200 OK
echo $responseBody;
}
+ protected function sendBadRequestResponse () {
+ header("HTTP/1.0 400 Bad Request");
+ http_response_code(400);
+ }
+
protected function sendInvalidMethodResponse () {
header("HTTP/1.0 405 Method Not Allowed");
http_response_code(405);
}
+ protected function sendInternalServerErrorResponse () {
+ header("HTTP/1.0 500 Internal Server Error");
+ http_response_code(500);
+ }
+
}
diff --git a/src/Service.php b/src/Service.php
index 0cce7a8..0b5c45d 100644
--- a/src/Service.php
+++ b/src/Service.php
@@ -1,64 +1,75 @@
<?php
namespace Nasqueron\Api\ServersLog;
use Dotenv\Dotenv;
class Service extends BaseService {
///
/// Initialisation
///
public function __construct () {
$this->loadEnvironment();
}
///
/// Controller
///
private function isAliveRequest() : bool {
return
$_SERVER['REQUEST_METHOD'] === "GET"
&&
$_SERVER['DOCUMENT_URI'] === '/status';
}
public function handle () : void {
if ($this->isAliveRequest()) {
$this->sendSuccessResponse("ALIVE");
} elseif ($_SERVER['REQUEST_METHOD'] === "PUT") {
$body = $this->getBodyObject();
$this->put($body);
} else {
$this->sendInvalidMethodResponse();
}
}
public function put ($data) : void {
+ if ($data === NULL) {
+ $this->sendBadRequestResponse();
+ return;
+ }
+
+ $log = self::getServersLogFile();
+ if (!$log) {
+ $this->sendInternalServerError();
+ return;
+ }
+
Log::addEntryToJSONFile(
- self::getServersLogFile(),
+ $log,
LogEntry::fromJSON($data)
);
$this->sendSuccessResponse();
}
///
/// Helper methods
///
private function loadEnvironment () : void {
$env = new Dotenv(__DIR__);
if (file_exists(__DIR__ . '/.env')) {
$env->load();
}
$env->required('SERVERS_LOG_FILE');
}
private static function getServersLogFile () : string {
return getenv('SERVERS_LOG_FILE');
}
}

File Metadata

Mime Type
text/x-diff
Expires
Sat, Mar 21, 05:37 (1 d, 7 h)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
3546484
Default Alt Text
(2 KB)

Event Timeline