From dea72c711ff740b3b829d238fcf85648465143a0 Mon Sep 17 00:00:00 2001 From: VirtualTam Date: Sat, 12 Jan 2019 23:55:38 +0100 Subject: Optimize and cleanup imports Signed-off-by: VirtualTam --- application/api/ApiMiddleware.php | 3 +- application/api/ApiUtils.php | 2 +- application/api/controllers/ApiController.php | 7 ++- application/api/controllers/History.php | 69 ---------------------- application/api/controllers/HistoryController.php | 69 ++++++++++++++++++++++ application/api/controllers/Tags.php | 1 - .../api/exceptions/ApiLinkNotFoundException.php | 2 - .../api/exceptions/ApiTagNotFoundException.php | 2 - 8 files changed, 75 insertions(+), 80 deletions(-) delete mode 100644 application/api/controllers/History.php create mode 100644 application/api/controllers/HistoryController.php (limited to 'application/api') diff --git a/application/api/ApiMiddleware.php b/application/api/ApiMiddleware.php index a2101f29..5ffb8c6d 100644 --- a/application/api/ApiMiddleware.php +++ b/application/api/ApiMiddleware.php @@ -1,9 +1,8 @@ history->getHistory(); - - // Return history operations from the {offset}th, starting from {since}. - $since = \DateTime::createFromFormat(\DateTime::ATOM, $request->getParam('since')); - $offset = $request->getParam('offset'); - if (empty($offset)) { - $offset = 0; - } elseif (ctype_digit($offset)) { - $offset = (int) $offset; - } else { - throw new ApiBadParametersException('Invalid offset'); - } - - // limit parameter is either a number of links or 'all' for everything. - $limit = $request->getParam('limit'); - if (empty($limit)) { - $limit = count($history); - } elseif (ctype_digit($limit)) { - $limit = (int) $limit; - } else { - throw new ApiBadParametersException('Invalid limit'); - } - - $out = []; - $i = 0; - foreach ($history as $entry) { - if ((! empty($since) && $entry['datetime'] <= $since) || count($out) >= $limit) { - break; - } - if (++$i > $offset) { - $out[$i] = $entry; - $out[$i]['datetime'] = $out[$i]['datetime']->format(\DateTime::ATOM); - } - } - $out = array_values($out); - - return $response->withJson($out, 200, $this->jsonStyle); - } -} diff --git a/application/api/controllers/HistoryController.php b/application/api/controllers/HistoryController.php new file mode 100644 index 00000000..9afcfa26 --- /dev/null +++ b/application/api/controllers/HistoryController.php @@ -0,0 +1,69 @@ +history->getHistory(); + + // Return history operations from the {offset}th, starting from {since}. + $since = \DateTime::createFromFormat(\DateTime::ATOM, $request->getParam('since')); + $offset = $request->getParam('offset'); + if (empty($offset)) { + $offset = 0; + } elseif (ctype_digit($offset)) { + $offset = (int) $offset; + } else { + throw new ApiBadParametersException('Invalid offset'); + } + + // limit parameter is either a number of links or 'all' for everything. + $limit = $request->getParam('limit'); + if (empty($limit)) { + $limit = count($history); + } elseif (ctype_digit($limit)) { + $limit = (int) $limit; + } else { + throw new ApiBadParametersException('Invalid limit'); + } + + $out = []; + $i = 0; + foreach ($history as $entry) { + if ((! empty($since) && $entry['datetime'] <= $since) || count($out) >= $limit) { + break; + } + if (++$i > $offset) { + $out[$i] = $entry; + $out[$i]['datetime'] = $out[$i]['datetime']->format(\DateTime::ATOM); + } + } + $out = array_values($out); + + return $response->withJson($out, 200, $this->jsonStyle); + } +} diff --git a/application/api/controllers/Tags.php b/application/api/controllers/Tags.php index 6dd78750..82f3ef74 100644 --- a/application/api/controllers/Tags.php +++ b/application/api/controllers/Tags.php @@ -4,7 +4,6 @@ namespace Shaarli\Api\Controllers; use Shaarli\Api\ApiUtils; use Shaarli\Api\Exceptions\ApiBadParametersException; -use Shaarli\Api\Exceptions\ApiLinkNotFoundException; use Shaarli\Api\Exceptions\ApiTagNotFoundException; use Slim\Http\Request; use Slim\Http\Response; diff --git a/application/api/exceptions/ApiLinkNotFoundException.php b/application/api/exceptions/ApiLinkNotFoundException.php index c727f4f0..7c2bb56e 100644 --- a/application/api/exceptions/ApiLinkNotFoundException.php +++ b/application/api/exceptions/ApiLinkNotFoundException.php @@ -2,8 +2,6 @@ namespace Shaarli\Api\Exceptions; -use Slim\Http\Response; - /** * Class ApiLinkNotFoundException * diff --git a/application/api/exceptions/ApiTagNotFoundException.php b/application/api/exceptions/ApiTagNotFoundException.php index eee152fe..66ace8bf 100644 --- a/application/api/exceptions/ApiTagNotFoundException.php +++ b/application/api/exceptions/ApiTagNotFoundException.php @@ -2,8 +2,6 @@ namespace Shaarli\Api\Exceptions; -use Slim\Http\Response; - /** * Class ApiTagNotFoundException * -- cgit v1.2.3