Issues (6)

src/RemServer/RemServerController.php (3 issues)

1
<?php
2
3
namespace Anax\RemServer;
4
5
use Anax\Commons\ContainerInjectableInterface;
6
use Anax\Commons\ContainerInjectableTrait;
7
8
/**
9
 * A controller for the REM Server.
10
 */
11
class RemServerController implements ContainerInjectableInterface
12
{
13
    use ContainerInjectableTrait;
14
15
16
17
    /**
18
     * Initiate the REM server before each action, if it has not already
19
     * some dataset(s).
20
     *
21
     * @return void
22
     */
23 21
    public function initialize() : void
24
    {
25 21
        $rem = $this->di->get("remserver");
26
27 21
        if (!$rem->hasDataset()) {
28 13
            $rem->init();
29
        }
30 21
    }
31
32
33
34
    /**
35
     * Init or re-init the REM Server.
36
     *
37
     * @return array
38
     */
39 9
    public function initActionGet() : array
40
    {
41 9
        $rem = $this->di->get("remserver");
42 9
        $rem->init();
43
        $json = [
44 9
            "message" => "The session is initiated with the default dataset(s).",
45 9
            "dataset" => $rem->getDefaultDataset(),
46
        ];
47 9
        return [$json];
48
    }
49
50
51
52
    /**
53
     * Get a dataset $key or parts of it by using the querystring.
54
     *
55
     * @param string $dataset identifier for the dataset
56
     *
57
     * @return array
58
     */
59 5
    public function getDataset($dataset) : array
60
    {
61 5
        $request = $this->di->get("request");
62 5
        $dataset = $this->di->get("remserver")->getDataset($dataset);
63 5
        $offset  = $request->getGet("offset", 0);
64 5
        $limit   = $request->getGet("limit", 25);
65
        $json = [
66 5
            "data" => array_slice($dataset, $offset, $limit),
67 5
            "offset" => $offset,
68 5
            "limit" => $limit,
69 5
            "total" => count($dataset)
70
        ];
71 5
        return [$json];
72
    }
73
74
75
76
    /**
77
     * Get one item from the dataset.
78
     *
79
     * @param string $dataset identifier for the dataset
80
     * @param int    $itemId  for the item to get
81
     *
82
     * @return array
83
     */
84 7
    public function getItem(string $dataset, int $itemId) : array
85
    {
86 7
        $item = $this->di->get("remserver")->getItem($dataset, $itemId);
87 7
        if (!$item) {
88 2
            return [["message" => "The item is not found."]];
89
        }
90 6
        return [$item];
91
    }
92
93
94
95
    /**
96
     * Create a new item by getting the entry from the request body and add
97
     * to the dataset.
98
     *
99
     * @param string $dataset identifier for the dataset
100
     *
101
     * @return array
102
     */
103 4
    public function postItem(string $dataset) : array
104
    {
105
        try {
106 4
            $entry = $this->di->get("request")->getBodyAsJson();
107 2
        } catch (\JsonException $e) {
108
            return [
109 2
                ["message" => "500. HTTP request body is not an object/array or valid JSON."],
110
                500
111
            ];
112
        }
113
114 2
        $item = $this->di->get("remserver")->addItem($dataset, $entry);
115 2
        return [$item];
116
    }
117
118
119
120
    /**
121
     * Upsert/replace an item in the dataset, entry is taken from request body.
122
     *
123
     * @param string $dataset for the dataset
124
     * @param int    $itemId  for the item to delete
125
     *
126
     * @return void
127
     */
128 4
    public function putItem(string $dataset, int $itemId) : array
129
    {
130
        try {
131 4
            $entry = $this->di->get("request")->getBodyAsJson();
132 2
        } catch (\JsonException $e) {
133
            return [
0 ignored issues
show
Bug Best Practice introduced by
The expression return array(array('mess... or valid JSON.'), 500) returns the type array<integer,array<string,string>|integer> which is incompatible with the documented return type void.
Loading history...
134 2
                ["message" => "500. HTTP request body is not an object/array or valid JSON."],
135
                500
136
            ];
137
        }
138
139 2
        $item = $this->di->get("remserver")->upsertItem($dataset, $itemId, $entry);
140 2
        return [$item];
0 ignored issues
show
Bug Best Practice introduced by
The expression return array($item) returns the type array which is incompatible with the documented return type void.
Loading history...
141
    }
142
143
144
145
    /**
146
     * Delete an item from the dataset.
147
     *
148
     * @param string $dataset for the dataset
149
     * @param int    $itemId  for the item to delete
150
     *
151
     * @return array
152
     */
153 3
    public function deleteItem(string $dataset, int $itemId) : array
154
    {
155 3
        $this->di->get("remserver")->deleteItem($dataset, $itemId);
156
        $json = [
157 3
            "message" => "Item id '$itemId' was deleted from dataset '$dataset'.",
158
        ];
159 3
        return [$json];
160
    }
161
162
163
164
    /**
165
     * Show a message that the route is unsupported, a local 404.
166
     *
167
     * @return void
168
     */
169 1
    public function catchAll()
170
    {
171 1
        return [["message" => "404. The api does not support that."], 404];
0 ignored issues
show
Bug Best Practice introduced by
The expression return array(array('mess...t support that.'), 404) returns the type array<integer,array<string,string>|integer> which is incompatible with the documented return type void.
Loading history...
172
    }
173
}
174