Completed
Pull Request — develop (#17)
by Patrick
04:03 queued 55s
created

DataTableAPI   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 146
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 0
Metric Value
dl 0
loc 146
rs 10
c 0
b 0
f 0
wmc 24
lcom 1
cbo 5

13 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 readEntries() 0 12 2
A createEntry() 0 11 2
A readEntry() 0 17 3
A updateEntry() 0 21 4
A deleteEntry() 0 20 4
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
    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...
70
    {
71
        if($this->canRead($request) === false)
72
        {
73
            return $response->withStatus(401);
74
        }
75
        $dataTable = $this->getDataTable();
76
        $odata = $request->getAttribute('odata', new \ODataParams(array()));
77
        $areas = $dataTable->read($odata->filter, $odata->select, $odata->top,
78
                                  $odata->skip, $odata->orderby);
79
        return $response->withJson($areas);
80
    }
81
82
    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...
83
    {
84
        if($this->canCreate($request) === false)
85
        {
86
            return $response->withStatus(401);
87
        }
88
        $dataTable = $this->getDataTable();
89
        $obj = $request->getParsedBody();
90
        $ret = $dataTable->create($obj);
91
        return $response->withJson($ret);
92
    }
93
94
    public function readEntry($request, $response, $args)
95
    {
96
        if($this->canRead($request) === false)
97
        {
98
            return $response->withStatus(401);
99
        }
100
        $dataTable = $this->getDataTable();
101
        $odata = $request->getAttribute('odata', new \ODataParams(array()));
102
        $filter = $this->getFilterForPrimaryKey($args['name']);
103
        $areas = $dataTable->read($filter, $odata->select, $odata->top,
104
                                  $odata->skip, $odata->orderby);
105
        if(empty($areas))
106
        {
107
            return $response->withStatus(404);
108
        }
109
        return $response->withJson($areas[0]);
110
    }
111
112
    public function updateEntry($request, $response, $args)
113
    {
114
        if($this->canRead($request) === false)
115
        {
116
            return $response->withStatus(401);
117
        }
118
        $filter = $this->getFilterForPrimaryKey($args['name']);
119
        $dataTable = $this->getDataTable();
120
        $entry = $dataTable->read($filter);
121
        if(empty($entry))
122
        {
123
            return $response->withStatus(404);
124
        }
125
        if($this->canUpdate($request, $entry) === false)
126
        {
127
            return $response->withStatus(401);
128
        }
129
        $obj = $request->getParsedBody();
130
        $ret = $dataTable->update($filter, $obj);
131
        return $response->withJson($ret);
132
    }
133
134
    public function deleteEntry($request, $response, $args)
135
    {
136
        if($this->canRead($request) === false)
137
        {
138
            return $response->withStatus(401);
139
        }
140
        $filter = $this->getFilterForPrimaryKey($args['name']);
141
        $dataTable = $this->getDataTable();
142
        $entry = $dataTable->read($filter);
143
        if(empty($entry))
144
        {
145
            return $response->withStatus(404);
146
        }
147
        if($this->canDelete($request, $entry) === false)
148
        {
149
            return $response->withStatus(401);
150
        }
151
        $ret = $dataTable->delete($filter);
152
        return $response->withJson($ret);
153
    }
154
}
155