BurningFlipside /
CommonCode
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 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
|
|||
| 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
|
|||
| 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
|
|||
| 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
|
|||
| 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
|
|||
| 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 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.