Completed
Push — master ( b809f6...7f2f7a )
by Mikael
01:42
created

RemServerController::deleteItem()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 8
Ratio 100 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 8
loc 8
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
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 View Code Duplication
    public function initActionGet() : array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public function getItem(string $dataset, int $itemId) : array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
    public function postItem(string $dataset) : array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
104
    {
105
        try {
106 4
            $entry = $this->di->get("request")->getBodyAsJson();
107 2
        } catch (\Anax\Request\Exception $e) {
0 ignored issues
show
Bug introduced by
The class Anax\Request\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
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 View Code Duplication
    public function putItem(string $dataset, int $itemId) : array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
129
    {
130
        try {
131 4
            $entry = $this->di->get("request")->getBodyAsJson();
132 2
        } catch (\Anax\Request\Exception $e) {
0 ignored issues
show
Bug introduced by
The class Anax\Request\Exception does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
133
            return [
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];
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 View Code Duplication
    public function deleteItem(string $dataset, int $itemId) : array
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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(...$args)
0 ignored issues
show
Unused Code introduced by
The parameter $args is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
170
    {
171 1
        return [["message" => "404. The api does not support that."], 404];
172
    }
173
}
174