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 | if(method_exists($this, 'processEntry')) |
||
| 80 | { |
||
| 81 | $count = count($areas); |
||
| 82 | for($i = 0; $i < $count; $i++) |
||
| 83 | { |
||
| 84 | $areas[$i] = $this->processEntry($areas[$i]); |
||
|
0 ignored issues
–
show
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...
|
|||
| 85 | } |
||
| 86 | } |
||
| 87 | return $response->withJson($areas); |
||
| 88 | } |
||
| 89 | |||
| 90 | public function createEntry($request, $response, $args) |
||
|
0 ignored issues
–
show
|
|||
| 91 | { |
||
| 92 | if($this->canCreate($request) === false) |
||
| 93 | { |
||
| 94 | return $response->withStatus(401); |
||
| 95 | } |
||
| 96 | $dataTable = $this->getDataTable(); |
||
| 97 | $obj = $request->getParsedBody(); |
||
| 98 | $ret = $dataTable->create($obj); |
||
| 99 | return $response->withJson($ret); |
||
| 100 | } |
||
| 101 | |||
| 102 | public function readEntry($request, $response, $args) |
||
| 103 | { |
||
| 104 | if($this->canRead($request) === false) |
||
| 105 | { |
||
| 106 | return $response->withStatus(401); |
||
| 107 | } |
||
| 108 | $dataTable = $this->getDataTable(); |
||
| 109 | $odata = $request->getAttribute('odata', new \ODataParams(array())); |
||
| 110 | $filter = $this->getFilterForPrimaryKey($args['name']); |
||
| 111 | $areas = $dataTable->read($filter, $odata->select, $odata->top, |
||
| 112 | $odata->skip, $odata->orderby); |
||
| 113 | if(empty($areas)) |
||
| 114 | { |
||
| 115 | return $response->withStatus(404); |
||
| 116 | } |
||
| 117 | if(method_exists($this, 'processEntry')) |
||
| 118 | { |
||
| 119 | $areas[0] = $this->processEntry($areas[0]); |
||
|
0 ignored issues
–
show
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...
|
|||
| 120 | } |
||
| 121 | return $response->withJson($areas[0]); |
||
| 122 | } |
||
| 123 | |||
| 124 | public function updateEntry($request, $response, $args) |
||
| 125 | { |
||
| 126 | if($this->canRead($request) === false) |
||
| 127 | { |
||
| 128 | return $response->withStatus(401); |
||
| 129 | } |
||
| 130 | $filter = $this->getFilterForPrimaryKey($args['name']); |
||
| 131 | $dataTable = $this->getDataTable(); |
||
| 132 | $entry = $dataTable->read($filter); |
||
| 133 | if(empty($entry)) |
||
| 134 | { |
||
| 135 | return $response->withStatus(404); |
||
| 136 | } |
||
| 137 | if($this->canUpdate($request, $entry) === false) |
||
| 138 | { |
||
| 139 | return $response->withStatus(401); |
||
| 140 | } |
||
| 141 | $obj = $request->getParsedBody(); |
||
| 142 | $ret = $dataTable->update($filter, $obj); |
||
| 143 | return $response->withJson($ret); |
||
| 144 | } |
||
| 145 | |||
| 146 | public function deleteEntry($request, $response, $args) |
||
| 147 | { |
||
| 148 | if($this->canRead($request) === false) |
||
| 149 | { |
||
| 150 | return $response->withStatus(401); |
||
| 151 | } |
||
| 152 | $filter = $this->getFilterForPrimaryKey($args['name']); |
||
| 153 | $dataTable = $this->getDataTable(); |
||
| 154 | $entry = $dataTable->read($filter); |
||
| 155 | if(empty($entry)) |
||
| 156 | { |
||
| 157 | return $response->withStatus(404); |
||
| 158 | } |
||
| 159 | if($this->canDelete($request, $entry) === false) |
||
| 160 | { |
||
| 161 | return $response->withStatus(401); |
||
| 162 | } |
||
| 163 | $ret = $dataTable->delete($filter); |
||
| 164 | return $response->withJson($ret); |
||
| 165 | } |
||
| 166 | } |
||
| 167 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.