Completed
Pull Request — master (#109)
by Patrick
10:35
created

DataTableAPI::readEntries()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 9
nop 3
dl 0
loc 29
rs 8.8337
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
    public function __construct($dataSetName, $dataTableName, $primaryKeyName = false)
16
    {
17
        $this->dataSetName    = $dataSetName;
18
        $this->dataTableName  = $dataTableName;
19
        $this->primaryKeyName = $primaryKeyName;
20
    }
21
22
    public function setup($app)
23
    {
24
        $app->get('[/]', array($this, 'readEntries'));
25
        $app->post('[/]', array($this, 'createEntry'));
26
        if($this->primaryKeyName !== false)
27
        {
28
            $app->get('/{name}[/]', array($this, 'readEntry'));
29
            $app->patch('/{name}[/]', array($this, 'updateEntry'));
30
            $app->delete('/{name}[/]', array($this, 'deleteEntry'));
31
        }
32
    }
33
34
    protected function getDataTable()
35
    {
36
        return \DataSetFactory::getDataTableByNames($this->dataSetName, $this->dataTableName);
37
    }
38
39
    protected function canRead($request)
40
    {
41
        $this->validateLoggedIn($request);
42
        //validateLoggedIn is fatal if not logged in...
43
        return true;
44
    }
45
46
    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...
47
    {
48
        //Must be overriden in a child class to allow create
49
        return false;
50
    }
51
52
    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...
53
    {
54
        //Must be overriden in a child class to allow update
55
        return false;
56
    }
57
58
    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...
59
    {
60
        //Must be overriden in a child class to allow update
61
        return false;
62
    }
63
64
    protected function getFilterForPrimaryKey($value)
65
    {
66
        return new \Data\Filter($this->primaryKeyName." eq '$value'");
67
    }
68
69
    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...
70
    {
71
        return false;
72
    }
73
74
    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...
75
    {
76
        return true;
77
    }
78
79
    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...
80
    {
81
        return true;
82
    }
83
84
    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...
85
    {
86
        if($this->canRead($request) === false)
87
        {
88
            return $response->withStatus(401);
89
        }
90
        $dataTable = $this->getDataTable();
91
        $odata = $request->getAttribute('odata', new \ODataParams(array()));
92
        $params = $this->manipulateParameters($request, $odata);
93
        $areas = $dataTable->read($odata->filter, $odata->select, $odata->top,
94
                                  $odata->skip, $odata->orderby, $params);
95
        if($areas === false)
96
        {
97
            $areas = array();
98
        }
99
        if(method_exists($this, 'processEntry'))
100
        {
101
            $count = count($areas);
102
            for($i = 0; $i < $count; $i++)
103
            {
104
                $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...
105
            }
106
        }
107
        if($odata->count)
108
        {
109
            $areas = array('@odata.count'=>count($areas), 'value'=>$areas);
110
        }
111
        return $response->withJson($areas);
112
    }
113
114
    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...
115
    {
116
        if($this->canCreate($request) === false)
117
        {
118
            return $response->withStatus(401);
119
        }
120
        $dataTable = $this->getDataTable();
121
        $obj = $request->getParsedBody();
122
        if($obj == NULL)
123
        {
124
            $obj = json_decode($request->getBody()->getContents(), true);
125
        }
126
        if($this->validateCreate($obj, $request) === false)
127
        {
128
            return $response->withStatus(400);
129
        }
130
        $ret = $dataTable->create($obj);
131
        return $response->withJson($ret);
132
    }
133
134
    public function readEntry($request, $response, $args)
135
    {
136
        if($this->canRead($request) === false)
137
        {
138
            return $response->withStatus(401);
139
        }
140
        $dataTable = $this->getDataTable();
141
        $odata = $request->getAttribute('odata', new \ODataParams(array()));
142
        $filter = $this->getFilterForPrimaryKey($args['name']);
143
        $areas = $dataTable->read($filter, $odata->select, $odata->top,
144
                                  $odata->skip, $odata->orderby);
145
        if(empty($areas))
146
        {
147
            return $response->withStatus(404);
148
        }
149
        if(method_exists($this, 'processEntry'))
150
        {
151
            $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...
152
        }
153
        return $response->withJson($areas[0]);
154
    }
155
156
    public function updateEntry($request, $response, $args)
157
    {
158
        if($this->canRead($request) === false)
159
        {
160
            return $response->withStatus(401);
161
        }
162
        $filter = $this->getFilterForPrimaryKey($args['name']);
163
        $dataTable = $this->getDataTable();
164
        $entry = $dataTable->read($filter);
165
        if(empty($entry))
166
        {
167
            return $response->withStatus(404);
168
        }
169
        if(count($entry) === 1 && isset($entry[0]))
170
        {
171
            $entry = $entry[0];
172
        }
173
        if($this->canUpdate($request, $entry) === false)
174
        {
175
            return $response->withStatus(401);
176
        }
177
        $obj = $request->getParsedBody();
178
        if($obj === null)
179
        {
180
            $request->getBody()->rewind();
181
            $obj = $request->getBody()->getContents();
182
            $tmp = json_decode($obj, true);
183
            if($tmp !== null)
184
            {
185
                $obj = $tmp;
186
            }
187
        }
188
        if($this->validateUpdate($obj, $request, $entry) === false)
189
        {
190
            return $response->withStatus(400);
191
        }
192
        $ret = $dataTable->update($filter, $obj);
193
        return $response->withJson($ret);
194
    }
195
196
    public function deleteEntry($request, $response, $args)
197
    {
198
        if($this->canRead($request) === false)
199
        {
200
            return $response->withStatus(401);
201
        }
202
        $filter = $this->getFilterForPrimaryKey($args['name']);
203
        $dataTable = $this->getDataTable();
204
        $entry = $dataTable->read($filter);
205
        if(empty($entry))
206
        {
207
            return $response->withStatus(404);
208
        }
209
        if($this->canDelete($request, $entry) === false)
210
        {
211
            return $response->withStatus(401);
212
        }
213
        $ret = $dataTable->delete($filter);
214
        return $response->withJson($ret);
215
    }
216
}
217