]> git.immae.eu Git - github/shaarli/Shaarli.git/blob - application/api/controllers/HistoryController.php
Apply PHP Code Beautifier on source code for linter automatic fixes
[github/shaarli/Shaarli.git] / application / api / controllers / HistoryController.php
1 <?php
2
3 namespace Shaarli\Api\Controllers;
4
5 use Shaarli\Api\Exceptions\ApiBadParametersException;
6 use Slim\Http\Request;
7 use Slim\Http\Response;
8
9 /**
10 * Class History
11 *
12 * REST API Controller: /history
13 *
14 * @package Shaarli\Api\Controllers
15 */
16 class HistoryController extends ApiController
17 {
18 /**
19 * Service providing operation regarding Shaarli datastore and settings.
20 *
21 * @param Request $request Slim request.
22 * @param Response $response Slim response.
23 *
24 * @return Response response.
25 *
26 * @throws ApiBadParametersException Invalid parameters.
27 */
28 public function getHistory($request, $response)
29 {
30 $history = $this->history->getHistory();
31
32 // Return history operations from the {offset}th, starting from {since}.
33 $since = \DateTime::createFromFormat(\DateTime::ATOM, $request->getParam('since'));
34 $offset = $request->getParam('offset');
35 if (empty($offset)) {
36 $offset = 0;
37 } elseif (ctype_digit($offset)) {
38 $offset = (int) $offset;
39 } else {
40 throw new ApiBadParametersException('Invalid offset');
41 }
42
43 // limit parameter is either a number of bookmarks or 'all' for everything.
44 $limit = $request->getParam('limit');
45 if (empty($limit)) {
46 $limit = count($history);
47 } elseif (ctype_digit($limit)) {
48 $limit = (int) $limit;
49 } else {
50 throw new ApiBadParametersException('Invalid limit');
51 }
52
53 $out = [];
54 $i = 0;
55 foreach ($history as $entry) {
56 if ((! empty($since) && $entry['datetime'] <= $since) || count($out) >= $limit) {
57 break;
58 }
59 if (++$i > $offset) {
60 $out[$i] = $entry;
61 $out[$i]['datetime'] = $out[$i]['datetime']->format(\DateTime::ATOM);
62 }
63 }
64 $out = array_values($out);
65
66 return $response->withJson($out, 200, $this->jsonStyle);
67 }
68 }