Completed
Push — master ( c63cba...594f96 )
by Patrick
02:11
created

DataTableAPI::postUpdateAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Http\Rest;
3
4
use \Psr\Http\Message\ServerRequestInterface as Request;
5
use \Psr\Http\Message\ResponseInterface as Response;
6
7
require 'vendor/autoload.php';
8
9
class DataTableAPI extends RestAPI
10
{
11
    protected $dataSetName;
12
    protected $dataTableName;
13
    protected $primaryKeyName;
14
15
    /**
16
     * Create the DataTableAPI for the given DataTable info
17
     *
18
     * @param string $datSetName The name of the DataSet used in the Settings
0 ignored issues
show
Documentation introduced by
There is no parameter named $datSetName. Did you maybe mean $dataSetName?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function. It has, however, found a similar but not annotated parameter which might be a good fit.

Consider the following example. The parameter $ireland is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $ireland
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was changed, but the annotation was not.

Loading history...
19
     * @param string $dataTableName The name of the table in the DataSet
20
     * @param string|false $primaryKeyName The table primary key. Must be specified for update/delete capable tables
21
     */
22
    public function __construct($dataSetName, $dataTableName, $primaryKeyName = false)
23
    {
24
        $this->dataSetName    = $dataSetName;
25
        $this->dataTableName  = $dataTableName;
26
        $this->primaryKeyName = $primaryKeyName;
27
    }
28
29
    public function setup($app)
30
    {
31
        $app->get('[/]', array($this, 'readEntries'));
32
        $app->post('[/]', array($this, 'createEntry'));
33
        if($this->primaryKeyName !== false)
34
        {
35
            $app->get('/{name}[/]', array($this, 'readEntry'));
36
            $app->patch('/{name}[/]', array($this, 'updateEntry'));
37
            $app->delete('/{name}[/]', array($this, 'deleteEntry'));
38
        }
39
    }
40
41
    protected function getDataTable()
42
    {
43
        return \DataSetFactory::getDataTableByNames($this->dataSetName, $this->dataTableName);
44
    }
45
46
    protected function canRead($request)
47
    {
48
        $this->validateLoggedIn($request);
49
        //validateLoggedIn is fatal if not logged in...
50
        return true;
51
    }
52
53
    protected function canCreate($request)
0 ignored issues
show
Unused Code introduced by
The parameter $request 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...
54
    {
55
        //Must be overriden in a child class to allow create
56
        return false;
57
    }
58
59
    protected function canUpdate($request, $entity)
0 ignored issues
show
Unused Code introduced by
The parameter $request 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...
Unused Code introduced by
The parameter $entity 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...
60
    {
61
        //Must be overriden in a child class to allow update
62
        return false;
63
    }
64
65
    protected function canDelete($request, $entity)
0 ignored issues
show
Unused Code introduced by
The parameter $request 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...
Unused Code introduced by
The parameter $entity 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...
66
    {
67
        //Must be overriden in a child class to allow update
68
        return false;
69
    }
70
71
    protected function getFilterForPrimaryKey($value)
72
    {
73
        return new \Data\Filter($this->primaryKeyName." eq '$value'");
74
    }
75
76
    protected function manipulateParameters($request, &$odata)
0 ignored issues
show
Unused Code introduced by
The parameter $request 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...
Unused Code introduced by
The parameter $odata 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...
77
    {
78
        return false;
79
    }
80
81
    protected function validateCreate(&$obj, $request)
0 ignored issues
show
Unused Code introduced by
The parameter $obj 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...
Unused Code introduced by
The parameter $request 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...
82
    {
83
        return true;
84
    }
85
86
    protected function validateUpdate(&$newObj, $request, $oldObj)
0 ignored issues
show
Unused Code introduced by
The parameter $newObj 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...
Unused Code introduced by
The parameter $request 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...
Unused Code introduced by
The parameter $oldObj 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...
87
    {
88
        return true;
89
    }
90
91
    protected function postUpdateAction($newObj, $request, $oldObj)
0 ignored issues
show
Unused Code introduced by
The parameter $newObj 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...
Unused Code introduced by
The parameter $request 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...
Unused Code introduced by
The parameter $oldObj 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...
92
    {
93
        return true;
94
    }
95
96
    protected function postDeleteAction($entry)
0 ignored issues
show
Unused Code introduced by
The parameter $entry 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...
97
    {
98
        return true;
99
    }
100
101
    public function readEntries($request, $response, $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...
102
    {
103
        if($this->canRead($request) === false)
104
        {
105
            return $response->withStatus(401);
106
        }
107
        $dataTable = $this->getDataTable();
108
        $odata = $request->getAttribute('odata', new \ODataParams(array()));
109
        $params = $this->manipulateParameters($request, $odata);
110
        $areas = $dataTable->read($odata->filter, $odata->select, $odata->top,
111
                                  $odata->skip, $odata->orderby, $params);
112
        if($areas === false)
113
        {
114
            $areas = array();
115
        }
116
        if(method_exists($this, 'processEntry'))
117
        {
118
            $count = count($areas);
119
            for($i = 0; $i < $count; $i++)
120
            {
121
                $areas[$i] = $this->processEntry($areas[$i], $request);
0 ignored issues
show
Bug introduced by
The method processEntry() does not seem to exist on object<Http\Rest\DataTableAPI>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
122
            }
123
        }
124
        $areas = array_values(array_filter($areas));
125
        if($odata->count)
126
        {
127
            $areas = array('@odata.count'=>count($areas), 'value'=>$areas);
128
        }
129
        return $response->withJson($areas);
130
    }
131
132
    public function createEntry($request, $response, $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...
133
    {
134
        if($this->canCreate($request) === false)
135
        {
136
            return $response->withStatus(401);
137
        }
138
        $dataTable = $this->getDataTable();
139
        $obj = $request->getParsedBody();
140
        if($obj == NULL)
141
        {
142
            $obj = json_decode($request->getBody()->getContents(), true);
143
        }
144
        if($this->validateCreate($obj, $request) === false)
145
        {
146
            return $response->withStatus(400);
147
        }
148
        $ret = $dataTable->create($obj);
149
        return $response->withJson($ret);
150
    }
151
152
    public function readEntry($request, $response, $args)
153
    {
154
        if($this->canRead($request) === false)
155
        {
156
            return $response->withStatus(401);
157
        }
158
        $dataTable = $this->getDataTable();
159
        $odata = $request->getAttribute('odata', new \ODataParams(array()));
160
        $filter = $this->getFilterForPrimaryKey($args['name']);
161
        $areas = $dataTable->read($filter, $odata->select, $odata->top,
162
                                  $odata->skip, $odata->orderby);
163
        if(empty($areas))
164
        {
165
            return $response->withStatus(404);
166
        }
167
        if(method_exists($this, 'processEntry'))
168
        {
169
            $areas[0] = $this->processEntry($areas[0], $request);
0 ignored issues
show
Bug introduced by
The method processEntry() does not seem to exist on object<Http\Rest\DataTableAPI>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
170
        }
171
        return $response->withJson($areas[0]);
172
    }
173
174
    public function updateEntry($request, $response, $args)
175
    {
176
        if($this->canRead($request) === false)
177
        {
178
            return $response->withStatus(401);
179
        }
180
        $filter = $this->getFilterForPrimaryKey($args['name']);
181
        $dataTable = $this->getDataTable();
182
        $entry = $dataTable->read($filter);
183
        if(empty($entry))
184
        {
185
            return $response->withStatus(404);
186
        }
187
        if(count($entry) === 1 && isset($entry[0]))
188
        {
189
            $entry = $entry[0];
190
        }
191
        if($this->canUpdate($request, $entry) === false)
192
        {
193
            return $response->withStatus(401);
194
        }
195
        $obj = $request->getParsedBody();
196
        if($obj === null)
197
        {
198
            $request->getBody()->rewind();
199
            $obj = $request->getBody()->getContents();
200
            $tmp = json_decode($obj, true);
201
            if($tmp !== null)
202
            {
203
                $obj = $tmp;
204
            }
205
        }
206
        if($this->validateUpdate($obj, $request, $entry) === false)
207
        {
208
            return $response->withStatus(400);
209
        }
210
        $ret = $dataTable->update($filter, $obj);
211
        if($ret)
212
        {
213
            $ret = $this->postUpdateAction($obj, $request, $entry);
214
        }
215
        return $response->withJson($ret);
216
    }
217
218
    public function deleteEntry($request, $response, $args)
219
    {
220
        if($this->canRead($request) === false)
221
        {
222
            return $response->withStatus(401);
223
        }
224
        $filter = $this->getFilterForPrimaryKey($args['name']);
225
        $dataTable = $this->getDataTable();
226
        $entry = $dataTable->read($filter);
227
        if(empty($entry))
228
        {
229
            return $response->withStatus(404);
230
        }
231
        $count = count($entry);
232
        for($i = 0; $i < $count; $i++)
233
        {
234
            if($this->canDelete($request, $entry[$i]) === false)
235
            {
236
                return $response->withStatus(401);
237
            }
238
        }
239
        $ret = $dataTable->delete($filter);
240
        if($ret)
241
        {
242
            $ret = $this->postDeleteAction($entry);
243
        }
244
        return $response->withJson($ret);
245
    }
246
}
247