Completed
Push — master ( 95eeda...c63cba )
by Patrick
02:15
created

DataTableAPI   A

Complexity

Total Complexity 42

Size/Duplication

Total Lines 229
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 229
rs 9.0399
c 0
b 0
f 0
wmc 42
lcom 1
cbo 5

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A setup() 0 11 2
A getDataTable() 0 4 1
A canRead() 0 6 1
A canCreate() 0 5 1
A canUpdate() 0 5 1
A canDelete() 0 5 1
A getFilterForPrimaryKey() 0 4 1
A manipulateParameters() 0 4 1
A validateCreate() 0 4 1
A validateUpdate() 0 4 1
A postDeleteAction() 0 4 1
B readEntries() 0 30 6
A createEntry() 0 19 4
A readEntry() 0 21 4
B updateEntry() 0 39 9
B deleteEntry() 0 28 6

How to fix   Complexity   

Complex Class

Complex classes like DataTableAPI often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use DataTableAPI, and based on these observations, apply Extract Interface, too.

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 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...
92
    {
93
        return true;
94
    }
95
96
    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...
97
    {
98
        if($this->canRead($request) === false)
99
        {
100
            return $response->withStatus(401);
101
        }
102
        $dataTable = $this->getDataTable();
103
        $odata = $request->getAttribute('odata', new \ODataParams(array()));
104
        $params = $this->manipulateParameters($request, $odata);
105
        $areas = $dataTable->read($odata->filter, $odata->select, $odata->top,
106
                                  $odata->skip, $odata->orderby, $params);
107
        if($areas === false)
108
        {
109
            $areas = array();
110
        }
111
        if(method_exists($this, 'processEntry'))
112
        {
113
            $count = count($areas);
114
            for($i = 0; $i < $count; $i++)
115
            {
116
                $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...
117
            }
118
        }
119
        $areas = array_values(array_filter($areas));
120
        if($odata->count)
121
        {
122
            $areas = array('@odata.count'=>count($areas), 'value'=>$areas);
123
        }
124
        return $response->withJson($areas);
125
    }
126
127
    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...
128
    {
129
        if($this->canCreate($request) === false)
130
        {
131
            return $response->withStatus(401);
132
        }
133
        $dataTable = $this->getDataTable();
134
        $obj = $request->getParsedBody();
135
        if($obj == NULL)
136
        {
137
            $obj = json_decode($request->getBody()->getContents(), true);
138
        }
139
        if($this->validateCreate($obj, $request) === false)
140
        {
141
            return $response->withStatus(400);
142
        }
143
        $ret = $dataTable->create($obj);
144
        return $response->withJson($ret);
145
    }
146
147
    public function readEntry($request, $response, $args)
148
    {
149
        if($this->canRead($request) === false)
150
        {
151
            return $response->withStatus(401);
152
        }
153
        $dataTable = $this->getDataTable();
154
        $odata = $request->getAttribute('odata', new \ODataParams(array()));
155
        $filter = $this->getFilterForPrimaryKey($args['name']);
156
        $areas = $dataTable->read($filter, $odata->select, $odata->top,
157
                                  $odata->skip, $odata->orderby);
158
        if(empty($areas))
159
        {
160
            return $response->withStatus(404);
161
        }
162
        if(method_exists($this, 'processEntry'))
163
        {
164
            $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...
165
        }
166
        return $response->withJson($areas[0]);
167
    }
168
169
    public function updateEntry($request, $response, $args)
170
    {
171
        if($this->canRead($request) === false)
172
        {
173
            return $response->withStatus(401);
174
        }
175
        $filter = $this->getFilterForPrimaryKey($args['name']);
176
        $dataTable = $this->getDataTable();
177
        $entry = $dataTable->read($filter);
178
        if(empty($entry))
179
        {
180
            return $response->withStatus(404);
181
        }
182
        if(count($entry) === 1 && isset($entry[0]))
183
        {
184
            $entry = $entry[0];
185
        }
186
        if($this->canUpdate($request, $entry) === false)
187
        {
188
            return $response->withStatus(401);
189
        }
190
        $obj = $request->getParsedBody();
191
        if($obj === null)
192
        {
193
            $request->getBody()->rewind();
194
            $obj = $request->getBody()->getContents();
195
            $tmp = json_decode($obj, true);
196
            if($tmp !== null)
197
            {
198
                $obj = $tmp;
199
            }
200
        }
201
        if($this->validateUpdate($obj, $request, $entry) === false)
202
        {
203
            return $response->withStatus(400);
204
        }
205
        $ret = $dataTable->update($filter, $obj);
206
        return $response->withJson($ret);
207
    }
208
209
    public function deleteEntry($request, $response, $args)
210
    {
211
        if($this->canRead($request) === false)
212
        {
213
            return $response->withStatus(401);
214
        }
215
        $filter = $this->getFilterForPrimaryKey($args['name']);
216
        $dataTable = $this->getDataTable();
217
        $entry = $dataTable->read($filter);
218
        if(empty($entry))
219
        {
220
            return $response->withStatus(404);
221
        }
222
        $count = count($entry);
223
        for($i = 0; $i < $count; $i++)
224
        {
225
            if($this->canDelete($request, $entry[$i]) === false)
226
            {
227
                return $response->withStatus(401);
228
            }
229
        }
230
        $ret = $dataTable->delete($filter);
231
        if($ret)
232
        {
233
            $ret = $this->postDeleteAction($entry);
234
        }
235
        return $response->withJson($ret);
236
    }
237
}
238