From 16f400447ac9423d40ca3f82c09c57fc80f94828 Mon Sep 17 00:00:00 2001 From: Bernd Bestel Date: Mon, 28 Dec 2020 19:38:52 +0100 Subject: [PATCH] Review --- controllers/SystemApiController.php | 25 ++++++++++++++++++++----- services/ApplicationService.php | 21 ++++++++++++--------- 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/controllers/SystemApiController.php b/controllers/SystemApiController.php index 4b47bbdb..bd12d253 100644 --- a/controllers/SystemApiController.php +++ b/controllers/SystemApiController.php @@ -45,11 +45,26 @@ class SystemApiController extends BaseApiController public function GetSystemTime(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args) { - $offset = 0; - $params = $request->getQueryParams(); - if (isset($params['offset'])) - $offset = $params['offset']; - return $this->ApiResponse($response, $this->getApplicationService()->GetSystemTime($offset)); + try + { + $offset = 0; + $params = $request->getQueryParams(); + if (isset($params['offset'])) + { + if (!filter_var($params['offset'], FILTER_VALIDATE_INT)) + { + throw new \Exception('Query parameter "offset" is not a valid integer'); + } + + $offset = $params['offset']; + } + + return $this->ApiResponse($response, $this->getApplicationService()->GetSystemTime($offset)); + } + catch (\Exception $ex) + { + return $this->GenericErrorResponse($response, $ex->getMessage()); + } } public function LogMissingLocalization(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, array $args) diff --git a/services/ApplicationService.php b/services/ApplicationService.php index 98fd31cd..819572ff 100644 --- a/services/ApplicationService.php +++ b/services/ApplicationService.php @@ -76,21 +76,24 @@ class ApplicationService extends BaseService ]; } - private static function convertToUtc(int $timestamp):string + private static function convertToUtc(int $timestamp): string { $dt = new \DateTime('now', new \DateTimeZone('UTC')); $dt->setTimestamp($timestamp); return $dt->format('Y-m-d H:i:s'); } - - private static function getSqliteLocaltime(int $offset):string + private static function getSqliteLocaltime(int $offset): string { $pdo = new \PDO('sqlite::memory:'); if ($offset > 0) + { return $pdo->query('SELECT datetime(\'now\', \'+' . $offset . ' seconds\', \'localtime\');')->fetch()[0]; + } else + { return $pdo->query('SELECT datetime(\'now\', \'' . $offset . ' seconds\', \'localtime\');')->fetch()[0]; + } } /** @@ -98,18 +101,18 @@ class ApplicationService extends BaseService * @param int $offset an offset in seconds to be applied * @return array */ - public function GetSystemTime(int $offset = 0):array + public function GetSystemTime(int $offset = 0): array { $timestamp = time() + $offset; $timeLocal = date('Y-m-d H:i:s', $timestamp); $timeUTC = self::convertToUtc($timestamp); return [ - 'timezone' => date_default_timezone_get(), - 'time_local' => $timeLocal, + 'timezone' => date_default_timezone_get(), + 'time_local' => $timeLocal, 'time_local_sqlite3' => self::getSqliteLocaltime($offset), - 'time_utc' => $timeUTC, - 'timestamp' => $timestamp, - 'offset' => $offset + 'time_utc' => $timeUTC, + 'timestamp' => $timestamp, + 'offset' => $offset ]; } }