Alive2212 /
LaravelSmartRestful
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace Alive2212\LaravelSmartRestful; |
||||
| 4 | |||||
| 5 | use Alive2212\ExcelHelper\ExcelHelper; |
||||
| 6 | use Alive2212\LaravelQueryHelper\QueryHelper; |
||||
|
0 ignored issues
–
show
|
|||||
| 7 | use Alive2212\LaravelRequestHelper\RequestHelper; |
||||
|
0 ignored issues
–
show
The type
Alive2212\LaravelRequestHelper\RequestHelper was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||||
| 8 | use Alive2212\LaravelSmartResponse\ResponseModel; |
||||
| 9 | use Alive2212\LaravelSmartResponse\SmartResponse\SmartResponse; |
||||
|
0 ignored issues
–
show
The type
Alive2212\LaravelSmartRe...tResponse\SmartResponse was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||||
| 10 | use Alive2212\LaravelStringHelper\StringHelper; |
||||
|
0 ignored issues
–
show
The type
Alive2212\LaravelStringHelper\StringHelper was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||||
| 11 | use App\Car; |
||||
|
0 ignored issues
–
show
The type
App\Car was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||||
| 12 | use App\Group; |
||||
|
0 ignored issues
–
show
The type
App\Group was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||||
| 13 | use App\Http\Controllers\Controller; |
||||
|
0 ignored issues
–
show
The type
App\Http\Controllers\Controller was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||||
| 14 | use App\Location; |
||||
|
0 ignored issues
–
show
The type
App\Location was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||||
| 15 | use Illuminate\Database\Eloquent\ModelNotFoundException; |
||||
| 16 | use Illuminate\Database\QueryException; |
||||
| 17 | use Illuminate\Http\Request; |
||||
| 18 | use Illuminate\Support\Facades\DB; |
||||
| 19 | use Illuminate\Support\MessageBag; |
||||
| 20 | use Illuminate\Support\Facades\Validator; |
||||
| 21 | use Mockery\Exception; |
||||
|
0 ignored issues
–
show
The type
Mockery\Exception was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||||
| 22 | |||||
| 23 | |||||
| 24 | abstract class BaseController extends Controller |
||||
| 25 | { |
||||
| 26 | /** |
||||
| 27 | * to use this class |
||||
| 28 | * create message list as messages in message file |
||||
| 29 | * override __constructor and define your model |
||||
| 30 | * define your rules for index,store and update |
||||
| 31 | */ |
||||
| 32 | |||||
| 33 | /** |
||||
| 34 | * permission types 'admin'|'branch'|'own'|'guest' |
||||
| 35 | * |
||||
| 36 | * @var string |
||||
| 37 | */ |
||||
| 38 | protected $permissionType = 'admin'; |
||||
| 39 | |||||
| 40 | /** |
||||
| 41 | * permission types 'admin'|'branch'|'own'|'guest' |
||||
| 42 | * |
||||
| 43 | * @var string |
||||
| 44 | */ |
||||
| 45 | protected $groupTitle = null; |
||||
| 46 | |||||
| 47 | /** |
||||
| 48 | * @var string |
||||
| 49 | */ |
||||
| 50 | protected $localPrefix = 'controller'; |
||||
| 51 | |||||
| 52 | /** |
||||
| 53 | * @var int |
||||
| 54 | */ |
||||
| 55 | protected $DEFAULT_RESULT_PER_PAGE = 15; |
||||
| 56 | |||||
| 57 | /** |
||||
| 58 | * @var int |
||||
| 59 | */ |
||||
| 60 | protected $DEFAULT_PAGE_NUMBER = 1; |
||||
| 61 | |||||
| 62 | /** |
||||
| 63 | * @var array |
||||
| 64 | */ |
||||
| 65 | protected $pivotFields = []; |
||||
| 66 | |||||
| 67 | /** |
||||
| 68 | * @var array |
||||
| 69 | */ |
||||
| 70 | protected $uniqueFields = []; |
||||
| 71 | |||||
| 72 | /** |
||||
| 73 | * @var bool|string |
||||
| 74 | */ |
||||
| 75 | protected $modelName; |
||||
| 76 | |||||
| 77 | /** |
||||
| 78 | * @var string |
||||
| 79 | */ |
||||
| 80 | protected $messagePrefix = 'messages.api.v1.'; |
||||
| 81 | |||||
| 82 | /** |
||||
| 83 | * this model |
||||
| 84 | */ |
||||
| 85 | protected $model; |
||||
| 86 | |||||
| 87 | /** |
||||
| 88 | * index request validator rules |
||||
| 89 | * |
||||
| 90 | * @var array |
||||
| 91 | */ |
||||
| 92 | protected $indexValidateArray = [ |
||||
| 93 | // |
||||
| 94 | ]; |
||||
| 95 | |||||
| 96 | /** |
||||
| 97 | * array of relationship for eager loading |
||||
| 98 | * |
||||
| 99 | * @var array |
||||
| 100 | */ |
||||
| 101 | protected $indexLoad = [ |
||||
| 102 | // |
||||
| 103 | ]; |
||||
| 104 | |||||
| 105 | /** |
||||
| 106 | * array of relationship for eager loading |
||||
| 107 | * |
||||
| 108 | * @var array |
||||
| 109 | */ |
||||
| 110 | protected $editLoad = [ |
||||
| 111 | // |
||||
| 112 | ]; |
||||
| 113 | |||||
| 114 | /** |
||||
| 115 | * array of relationship for eager loading |
||||
| 116 | * |
||||
| 117 | * @var array |
||||
| 118 | */ |
||||
| 119 | protected $updateLoad = [ |
||||
| 120 | // |
||||
| 121 | ]; |
||||
| 122 | |||||
| 123 | /** |
||||
| 124 | * array of relationship for eager loading |
||||
| 125 | * |
||||
| 126 | * @var array |
||||
| 127 | */ |
||||
| 128 | protected $storeLoad = [ |
||||
| 129 | // |
||||
| 130 | ]; |
||||
| 131 | |||||
| 132 | /** |
||||
| 133 | * store request validator rules |
||||
| 134 | * |
||||
| 135 | * @var array |
||||
| 136 | */ |
||||
| 137 | protected $storeValidateArray = [ |
||||
| 138 | // |
||||
| 139 | ]; |
||||
| 140 | |||||
| 141 | /** |
||||
| 142 | * update request validator rules |
||||
| 143 | * |
||||
| 144 | * @var array |
||||
| 145 | */ |
||||
| 146 | protected $updateValidateArray = [ |
||||
| 147 | // |
||||
| 148 | ]; |
||||
| 149 | |||||
| 150 | protected $middlewareParams = []; |
||||
| 151 | |||||
| 152 | /** |
||||
| 153 | * defaultController constructor. |
||||
| 154 | */ |
||||
| 155 | public function __construct() |
||||
| 156 | { |
||||
| 157 | // dd("I have closest relationship with all US & UK celebrities"); |
||||
| 158 | // init controller |
||||
| 159 | $this->initController(); |
||||
| 160 | |||||
| 161 | // set local language |
||||
| 162 | $this->setLocale(); |
||||
| 163 | } |
||||
| 164 | |||||
| 165 | abstract public function initController(); |
||||
| 166 | |||||
| 167 | /** |
||||
| 168 | * Display a listing of the resource. |
||||
| 169 | * |
||||
| 170 | * @param Request $request |
||||
| 171 | * @return string |
||||
| 172 | */ |
||||
| 173 | public function index(Request $request) |
||||
| 174 | { |
||||
| 175 | // handle permission |
||||
| 176 | list($request,$filters) = $this->handlePermission(__FUNCTION__, $request); |
||||
| 177 | |||||
| 178 | // create response model |
||||
| 179 | $response = new ResponseModel(); |
||||
| 180 | |||||
| 181 | $pageSize = $this->DEFAULT_RESULT_PER_PAGE; |
||||
| 182 | |||||
| 183 | $pageNumber = 1; |
||||
| 184 | |||||
| 185 | //set default pagination |
||||
| 186 | |||||
| 187 | //set page size |
||||
| 188 | if (!isset($request->toArray()['page']['size'])) { |
||||
| 189 | $pageSize = $this->DEFAULT_RESULT_PER_PAGE; |
||||
| 190 | } elseif (($request->get('page')['size']) == 0) { |
||||
| 191 | $pageSize = $this->DEFAULT_RESULT_PER_PAGE; |
||||
| 192 | } else { |
||||
| 193 | $pageSize = $request->get('page')['size']; |
||||
| 194 | } |
||||
| 195 | |||||
| 196 | //set page number |
||||
| 197 | if (!isset($request->get('page')['number'])) { |
||||
| 198 | $pageNumber = $this->DEFAULT_PAGE_NUMBER; |
||||
| 199 | } elseif (($request->get('page')['number']) == 0) { |
||||
| 200 | $pageNumber = $this->DEFAULT_PAGE_NUMBER; |
||||
| 201 | } else { |
||||
| 202 | $pageNumber = $request->get('page')['number']; |
||||
| 203 | } |
||||
| 204 | $request['page'] = $pageNumber; |
||||
| 205 | |||||
| 206 | |||||
| 207 | //set default ordering |
||||
| 208 | if (isset($request->toArray()['order_by'])) { |
||||
| 209 | if (is_null($request['order_by'])) { |
||||
| 210 | $request['order_by'] = "{\"field\":\"id\",\"operator\":\"Desc\"}"; |
||||
| 211 | } |
||||
| 212 | } |
||||
| 213 | |||||
| 214 | $validationErrors = $this->checkRequestValidation($request, $this->indexValidateArray); |
||||
| 215 | if ($validationErrors != null) { |
||||
| 216 | if (env('APP_DEBUG', false)) { |
||||
| 217 | $response->setData(collect($validationErrors->toArray())); |
||||
| 218 | } |
||||
| 219 | $response->setMessage($this->getTrans(__FUNCTION__, 'validation_failed')); |
||||
| 220 | $response->setStatus(false); |
||||
| 221 | $response->setError(99); |
||||
| 222 | return SmartResponse::response($response); |
||||
| 223 | } |
||||
| 224 | |||||
| 225 | try { |
||||
| 226 | $data = $request->get('query') != null ? |
||||
| 227 | $this->model |
||||
| 228 | ->whereKey(collect($this->model |
||||
| 229 | ->search(($request->get('query'))) |
||||
| 230 | ->raw())->get('ids')) : |
||||
| 231 | $this->model; |
||||
| 232 | |||||
| 233 | if (array_key_exists('file', $request->toArray())) { |
||||
| 234 | //TODO add relation on top if here and create a tree flatter array in array helper |
||||
| 235 | return (new ExcelHelper())->setOptions([ |
||||
|
0 ignored issues
–
show
Are you sure the usage of
new Alive2212\ExcelHelpe...ExcelFile()->download() targeting Alive2212\ExcelHelper\ExcelHelper::download() seems to always return null.
This check looks for function or method calls that always return null and whose return value is used. class A
{
function getObject()
{
return null;
}
}
$a = new A();
if ($a->getObject()) {
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. Loading history...
|
|||||
| 236 | 'store_format' => $request->get('file') == null ? 'xls' : $request->get('file'), |
||||
| 237 | 'download_format' => $request->get('file') == null ? 'xls' : $request->get('file'), |
||||
| 238 | ])->table($data->get()->toArray())->createExcelFile()->download(); |
||||
| 239 | } |
||||
| 240 | |||||
| 241 | // load relations |
||||
| 242 | if (count($this->indexLoad) > 0) { |
||||
| 243 | $data = $data->with($this->indexLoad); |
||||
| 244 | } |
||||
| 245 | |||||
| 246 | // filters by |
||||
| 247 | if (!is_null($request->get('filters'))) { |
||||
| 248 | $data = (new QueryHelper())->deepFilter($data, (new RequestHelper())->getCollectFromJson($request['filters'])); |
||||
| 249 | } |
||||
| 250 | |||||
| 251 | // order by |
||||
| 252 | if (!is_null($request->get('order_by'))) { |
||||
| 253 | $data = (new QueryHelper())->orderBy($data, (new RequestHelper())->getCollectFromJson($request['order_by'])); |
||||
| 254 | } |
||||
| 255 | |||||
| 256 | // return response |
||||
| 257 | $response->setData(collect($data->paginate($pageSize))); |
||||
| 258 | $response->setMessage($this->getTrans('index', 'successful')); |
||||
| 259 | return SmartResponse::response($response); |
||||
| 260 | |||||
| 261 | } catch (QueryException $exception) { |
||||
| 262 | |||||
| 263 | // return response |
||||
| 264 | $response->setData(collect($exception->getMessage())); |
||||
| 265 | $response->setError($exception->getCode()); |
||||
| 266 | $response->setMessage($this->getTrans('index', 'failed')); |
||||
| 267 | $response->setStatus(false); |
||||
| 268 | return SmartResponse::response($response); |
||||
| 269 | } |
||||
| 270 | } |
||||
| 271 | |||||
| 272 | public function checkRequestValidation(Request $request, $validationArray) |
||||
| 273 | { |
||||
| 274 | $requestParams = $request->toArray(); |
||||
| 275 | $validator = Validator::make($request->all(), $validationArray); |
||||
| 276 | if ($validator->fails()) { |
||||
| 277 | return $validator->errors(); |
||||
| 278 | } |
||||
| 279 | return null; |
||||
| 280 | } |
||||
| 281 | |||||
| 282 | /** |
||||
| 283 | * @param $status |
||||
| 284 | * @return mixed |
||||
| 285 | */ |
||||
| 286 | public function message($status) |
||||
| 287 | { |
||||
| 288 | $key = $this->messagePrefix . $this->modelName . '.' . debug_backtrace()[1]['function'] . '.' . $status; |
||||
| 289 | return $this->getMessageFromFile($key); |
||||
| 290 | } |
||||
| 291 | |||||
| 292 | /** |
||||
| 293 | * @param $key |
||||
| 294 | * @return mixed |
||||
| 295 | */ |
||||
| 296 | public function getMessageFromFile($key) |
||||
| 297 | { |
||||
| 298 | return config($key); |
||||
| 299 | } |
||||
| 300 | |||||
| 301 | /** |
||||
| 302 | * Show the form for creating a new resource. |
||||
| 303 | * |
||||
| 304 | * @return \Illuminate\Http\JsonResponse |
||||
| 305 | */ |
||||
| 306 | public function create() |
||||
| 307 | { |
||||
| 308 | // Create Response Model |
||||
| 309 | $response = new ResponseModel(); |
||||
| 310 | |||||
| 311 | // return response |
||||
| 312 | $response->setData(collect($this->model->getFillable())); |
||||
| 313 | $response->setMessage($this->getTrans('create', 'successful')); |
||||
| 314 | return SmartResponse::response($response); |
||||
| 315 | } |
||||
| 316 | |||||
| 317 | /** |
||||
| 318 | * Store a newly created resource in storage. |
||||
| 319 | * |
||||
| 320 | * @param \Illuminate\Http\Request $request |
||||
| 321 | * @return \Illuminate\Http\JsonResponse |
||||
| 322 | */ |
||||
| 323 | public function store(Request $request) |
||||
| 324 | { |
||||
| 325 | // // handle permission |
||||
| 326 | // $request = $this->handlePermission(__FUNCTION__, $request); |
||||
| 327 | |||||
| 328 | // Create Response Model |
||||
| 329 | $response = new ResponseModel(); |
||||
| 330 | |||||
| 331 | if (!isset($userId)) { |
||||
| 332 | $userId = 1; |
||||
| 333 | } |
||||
| 334 | |||||
| 335 | //add author id into the request if doesn't exist |
||||
| 336 | if (is_null($request->get('author_id'))) { |
||||
| 337 | $request['author_id'] = $userId; |
||||
| 338 | } |
||||
| 339 | |||||
| 340 | //add user id into the request if doesn't exist |
||||
| 341 | if (is_null($request->get('user_id'))) { |
||||
| 342 | $request['user_id'] = $userId; |
||||
| 343 | } |
||||
| 344 | |||||
| 345 | $validationErrors = $this->checkRequestValidation($request, $this->storeValidateArray); |
||||
| 346 | |||||
| 347 | if ($validationErrors != null) { |
||||
| 348 | if (env('APP_DEBUG', false)) { |
||||
| 349 | $response->setData(collect($validationErrors->toArray())); |
||||
| 350 | } |
||||
| 351 | $response->setMessage($this->getTrans(__FUNCTION__, 'validation_failed')); |
||||
| 352 | $response->setStatus(false); |
||||
| 353 | $response->setError(99); |
||||
| 354 | return SmartResponse::response($response); |
||||
| 355 | |||||
| 356 | } |
||||
| 357 | |||||
| 358 | try { |
||||
| 359 | // get result of model creation |
||||
| 360 | $result = $this->model->create($request->all()); |
||||
| 361 | // sync many to many relation |
||||
| 362 | foreach ($this->pivotFields as $pivotField) { |
||||
| 363 | if (collect($request[$pivotField])->count()) { |
||||
| 364 | $pivotField = (new StringHelper())->toCamel($pivotField); |
||||
| 365 | $this->model->find($result['id'])->$pivotField()->sync(json_decode($request[$pivotField])); |
||||
| 366 | } |
||||
| 367 | } |
||||
| 368 | $response->setMessage($this->getTrans('store', 'successful')); |
||||
| 369 | |||||
| 370 | |||||
| 371 | $response->setData($this->model |
||||
| 372 | ->where($this->model->getKeyName(), $result['id']) |
||||
| 373 | ->with(collect($this->updateLoad)->count() == 0 ? $this->indexLoad : $this->updateLoad) |
||||
| 374 | ->get()); |
||||
| 375 | |||||
| 376 | $response->setStatus(true); |
||||
| 377 | } catch (QueryException $exception) { |
||||
| 378 | $response->setError($exception->getCode()); |
||||
| 379 | $response->setMessage($this->getTrans('store', 'failed')); |
||||
| 380 | $response->setStatus(false); |
||||
| 381 | if (env('APP_DEBUG', false)) { |
||||
| 382 | $response->setData(collect($exception->getMessage())); |
||||
| 383 | } |
||||
| 384 | } |
||||
| 385 | return SmartResponse::response($response); |
||||
| 386 | } |
||||
| 387 | |||||
| 388 | /** |
||||
| 389 | * Display the specdefaultied resource. |
||||
| 390 | * |
||||
| 391 | * @param int $id |
||||
| 392 | * @return \Illuminate\Http\JsonResponse |
||||
| 393 | */ |
||||
| 394 | public function show($id) |
||||
| 395 | { |
||||
| 396 | $filters = []; |
||||
| 397 | // // handle permission |
||||
| 398 | // $filters = $this->handlePermission(__FUNCTION__); |
||||
| 399 | |||||
| 400 | // Create Response Model |
||||
| 401 | $response = new ResponseModel(); |
||||
| 402 | |||||
| 403 | // try to get data |
||||
| 404 | try { |
||||
| 405 | $response->setMessage($this->getTrans('show', 'successful')); |
||||
| 406 | |||||
| 407 | // add filter to get desired record |
||||
| 408 | array_push($filters, |
||||
| 409 | [$this->model->getKeyName(), '=', $id] |
||||
| 410 | ); |
||||
| 411 | $data = $this->model |
||||
| 412 | ->where($filters) |
||||
| 413 | ->get(); |
||||
| 414 | $response->setData($data); |
||||
| 415 | |||||
| 416 | // catch exception |
||||
| 417 | } catch (Exception $exception) { |
||||
| 418 | $response->setError($exception->getCode()); |
||||
| 419 | $response->setMessage($this->getTrans('show', 'failed')); |
||||
| 420 | $response->setStatus(false); |
||||
| 421 | if (env('APP_DEBUG', false)) { |
||||
| 422 | $response->setData(collect($exception->getMessage())); |
||||
| 423 | } |
||||
| 424 | } |
||||
| 425 | return SmartResponse::response($response); |
||||
| 426 | } |
||||
| 427 | |||||
| 428 | /** |
||||
| 429 | * Show the form for editing the specified resource. |
||||
| 430 | * |
||||
| 431 | * @param int $id |
||||
| 432 | * @return \Illuminate\Http\JsonResponse |
||||
| 433 | */ |
||||
| 434 | public function edit($id) |
||||
| 435 | { |
||||
| 436 | $filters = []; |
||||
| 437 | // // handle permission |
||||
| 438 | // $filters = $this->handlePermission(__FUNCTION__); |
||||
| 439 | |||||
| 440 | // Create Response Model |
||||
| 441 | $response = new ResponseModel(); |
||||
| 442 | |||||
| 443 | try { |
||||
| 444 | $response->setMessage($this->getTrans('edit', 'successful')); |
||||
| 445 | |||||
| 446 | // add filter to get desired record |
||||
| 447 | array_push($filters, |
||||
| 448 | [$this->model->getKeyName(), '=', $id] |
||||
| 449 | ); |
||||
| 450 | $data = $this->model |
||||
| 451 | ->where($filters) |
||||
| 452 | ->with(collect($this->editLoad)->count() == 0 ? $this->indexLoad : $this->editLoad) |
||||
| 453 | ->get(); |
||||
| 454 | $response->setData($data); |
||||
| 455 | |||||
| 456 | } catch (ModelNotFoundException $exception) { |
||||
| 457 | $response->setError($exception->getCode()); |
||||
|
0 ignored issues
–
show
It seems like
$exception->getCode() can also be of type integer; however, parameter $error of Alive2212\LaravelSmartRe...sponseModel::setError() does only seem to accept null, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 458 | $response->setMessage($this->getTrans('edit', 'failed')); |
||||
| 459 | $response->setStatus(false); |
||||
| 460 | if (env('APP_DEBUG', false)) { |
||||
| 461 | $response->setData(collect($exception->getMessage())); |
||||
| 462 | } |
||||
| 463 | |||||
| 464 | } |
||||
| 465 | |||||
| 466 | return SmartResponse::response($response); |
||||
| 467 | } |
||||
| 468 | |||||
| 469 | /** |
||||
| 470 | * Update the specified resource in storage. |
||||
| 471 | * |
||||
| 472 | * @param \Illuminate\Http\Request $request |
||||
| 473 | * @param int $id |
||||
| 474 | * @return \Illuminate\Http\JsonResponse |
||||
| 475 | */ |
||||
| 476 | public function update(Request $request, $id) |
||||
| 477 | { |
||||
| 478 | // Handle permission |
||||
| 479 | // $request = $this->handlePermission(__FUNCTION__,$request); |
||||
| 480 | // if ($request->has('permission_filters')){ |
||||
| 481 | // $filters = $request['permission_filters']; |
||||
| 482 | // }else{ |
||||
| 483 | $filters = []; |
||||
| 484 | // } |
||||
| 485 | |||||
| 486 | // Create Response Model |
||||
| 487 | $response = new ResponseModel(); |
||||
| 488 | |||||
| 489 | // Filters |
||||
| 490 | array_push($filters, |
||||
| 491 | [$this->model->getKeyName(), '=', $id] |
||||
| 492 | ); |
||||
| 493 | |||||
| 494 | $validationErrors = $this->checkRequestValidation($request, $this->updateValidateArray); |
||||
| 495 | if ($validationErrors != null) { |
||||
| 496 | if (env('APP_DEBUG', false)) { |
||||
| 497 | $response->setData(collect($validationErrors->toArray())); |
||||
| 498 | } |
||||
| 499 | $response->setMessage($this->getTrans(__FUNCTION__, 'validation_failed')); |
||||
| 500 | $response->setStatus(false); |
||||
| 501 | $response->setError(99); |
||||
| 502 | return SmartResponse::response($response); |
||||
| 503 | |||||
| 504 | } |
||||
| 505 | |||||
| 506 | try { |
||||
| 507 | // sync many to many relation |
||||
| 508 | foreach ($this->pivotFields as $pivotField) { |
||||
| 509 | if (collect($request[$pivotField])->count()) { |
||||
| 510 | $pivotMethod = (new StringHelper())->toCamel($pivotField); |
||||
| 511 | $this->model->findOrFail($id)->$pivotMethod()->sync(json_decode($request[$pivotField], true)); |
||||
| 512 | } |
||||
| 513 | } |
||||
| 514 | //get result of update |
||||
| 515 | $result = $this->model->where($filters)->firstOrFail()->update($request->all()); |
||||
| 516 | |||||
| 517 | // return response |
||||
| 518 | $response->setData($this->model |
||||
| 519 | ->where($filters) |
||||
| 520 | ->with(collect($this->updateLoad)->count() == 0 ? $this->indexLoad : $this->updateLoad) |
||||
| 521 | ->get()); |
||||
| 522 | $response->setMessage( |
||||
| 523 | $this->getTrans(__FUNCTION__, 'successful1') . |
||||
|
0 ignored issues
–
show
Are you sure
$this->getTrans(__FUNCTION__, 'successful1') of type null|string|array can be used in concatenation?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 524 | $result . |
||||
| 525 | $this->getTrans(__FUNCTION__, 'successful2') |
||||
|
0 ignored issues
–
show
Are you sure
$this->getTrans(__FUNCTION__, 'successful2') of type null|string|array can be used in concatenation?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 526 | ); |
||||
| 527 | |||||
| 528 | } catch (ModelNotFoundException $exception) { |
||||
| 529 | $response->setStatus(false); |
||||
| 530 | $response->setMessage($this->getTrans(__FUNCTION__, 'model_not_found')); |
||||
| 531 | $response->setError($exception->getCode()); |
||||
| 532 | if (env('APP_DEBUG', false)) { |
||||
| 533 | $response->setData(collect($exception->getMessage())); |
||||
| 534 | } |
||||
| 535 | |||||
| 536 | } catch (QueryException $exception) { |
||||
| 537 | $response->setStatus(false); |
||||
| 538 | $response->setMessage($this->getTrans(__FUNCTION__, 'failed')); |
||||
| 539 | $response->setError($exception->getCode()); |
||||
| 540 | if (env('APP_DEBUG', false)) { |
||||
| 541 | $response->setData(collect($exception->getMessage())); |
||||
| 542 | } |
||||
| 543 | |||||
| 544 | } |
||||
| 545 | |||||
| 546 | return SmartResponse::response($response); |
||||
| 547 | } |
||||
| 548 | |||||
| 549 | /** |
||||
| 550 | * Remove the specified resource from storage. |
||||
| 551 | * |
||||
| 552 | * @param int $id |
||||
| 553 | * @return \Illuminate\Http\JsonResponse |
||||
| 554 | */ |
||||
| 555 | public function destroy($id) |
||||
| 556 | { |
||||
| 557 | // Create Response Model |
||||
| 558 | $response = new ResponseModel(); |
||||
| 559 | |||||
| 560 | try { |
||||
| 561 | $response->setData(collect($this->model->findOrFail($id)->delete())); |
||||
| 562 | $response->setMessage($this->getTrans('destroy', 'successful')); |
||||
| 563 | |||||
| 564 | } catch (ModelNotFoundException $exception) { |
||||
| 565 | $response->setMessage($this->getTrans('destroy', 'successful')); |
||||
| 566 | $response->setStatus(false); |
||||
| 567 | $response->setError($exception->getCode()); |
||||
|
0 ignored issues
–
show
It seems like
$exception->getCode() can also be of type integer; however, parameter $error of Alive2212\LaravelSmartRe...sponseModel::setError() does only seem to accept null, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 568 | if (env('APP_DEBUG', false)) { |
||||
| 569 | $response->setData(collect($exception->getMessage())); |
||||
| 570 | } |
||||
| 571 | |||||
| 572 | } |
||||
| 573 | |||||
| 574 | return SmartResponse::response($response); |
||||
| 575 | } |
||||
| 576 | |||||
| 577 | /** |
||||
| 578 | * @param $method |
||||
| 579 | * @param $status |
||||
| 580 | * @return array|\Illuminate\Contracts\Translation\Translator|null|string |
||||
| 581 | */ |
||||
| 582 | public function getTrans($method, $status) |
||||
| 583 | { |
||||
| 584 | $className = array_last(explode('\\', get_class($this))); |
||||
| 585 | return trans('laravel_smart_restful::' . $this->localPrefix . '.' . $className . '.' . $method . '.' . $status); |
||||
| 586 | } |
||||
| 587 | |||||
| 588 | /** |
||||
| 589 | * set locale |
||||
| 590 | */ |
||||
| 591 | public function setLocale() |
||||
| 592 | { |
||||
| 593 | if (isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) { |
||||
| 594 | app('translator')->setLocale($_SERVER['HTTP_ACCEPT_LANGUAGE']); |
||||
| 595 | } |
||||
| 596 | } |
||||
| 597 | |||||
| 598 | /** |
||||
| 599 | * @param $functionName |
||||
| 600 | * @param Request|null $request |
||||
| 601 | * @param null $id |
||||
|
0 ignored issues
–
show
|
|||||
| 602 | * @param array $params |
||||
| 603 | * @return mixed |
||||
| 604 | */ |
||||
| 605 | public function handlePermission($functionName, Request $request = null, $id = null, $params = []) |
||||
| 606 | { |
||||
| 607 | $filters = null; |
||||
| 608 | |||||
| 609 | if (!is_null($request)) { |
||||
| 610 | if ($request->has('permission_type')) { |
||||
| 611 | $this->permissionType = $request['permission_type']; |
||||
| 612 | } |
||||
| 613 | } |
||||
| 614 | |||||
| 615 | $methodName = |
||||
| 616 | 'handle' . |
||||
| 617 | (new StringHelper())->upperFirstLetter($functionName) . |
||||
| 618 | (new StringHelper())->upperFirstLetter($this->permissionType) . |
||||
| 619 | 'Permission'; |
||||
| 620 | |||||
| 621 | return $this->$methodName($request, $id); |
||||
| 622 | } |
||||
| 623 | |||||
| 624 | /** |
||||
| 625 | * @param Request $request |
||||
| 626 | * @param $id |
||||
| 627 | * @return array |
||||
| 628 | */ |
||||
| 629 | public function handleIndexAdminPermission(Request $request, $id) |
||||
| 630 | { |
||||
| 631 | return [$request, null]; |
||||
| 632 | } |
||||
| 633 | |||||
| 634 | /** |
||||
| 635 | * @param Request $request |
||||
| 636 | * @param $id |
||||
| 637 | * @return array |
||||
| 638 | */ |
||||
| 639 | public function handleIndexBranchPermission(Request $request, $id) |
||||
| 640 | { |
||||
| 641 | if (isset($request['filters'])) { |
||||
| 642 | $filters = json_decode($request['filters'], true); |
||||
| 643 | } else { |
||||
| 644 | $filters = []; |
||||
| 645 | } |
||||
| 646 | |||||
| 647 | $filters = $this->addFilter($filters, 'owner_id', '=', auth()->id()); |
||||
| 648 | |||||
| 649 | if (!is_null($this->groupTitle)) { |
||||
| 650 | $filters = $this->addFilter($filters, 'group.title', '=', $this->groupTitle); |
||||
| 651 | } |
||||
| 652 | |||||
| 653 | $request['filters'] = json_encode($filters); |
||||
| 654 | return [$request, $filters]; |
||||
| 655 | } |
||||
| 656 | |||||
| 657 | /** |
||||
| 658 | * @param Request $request |
||||
| 659 | * @param $id |
||||
| 660 | * @return array |
||||
| 661 | */ |
||||
| 662 | public function handleIndexOwnPermission(Request $request, $id) |
||||
| 663 | { |
||||
| 664 | if (isset($request['filters'])) { |
||||
| 665 | $filters = json_decode($request['filters'], true); |
||||
| 666 | } else { |
||||
| 667 | $filters = []; |
||||
| 668 | } |
||||
| 669 | |||||
| 670 | $filters = $this->addFilter($filters, 'owner_id', '=', auth()->id()); |
||||
| 671 | |||||
| 672 | if (!is_null($this->groupTitle)) { |
||||
| 673 | $filters = $this->addFilter($filters, 'group.title', '=', $this->groupTitle); |
||||
| 674 | } |
||||
| 675 | |||||
| 676 | $request['filters'] = json_encode($filters); |
||||
| 677 | |||||
| 678 | return [$request, $filters]; |
||||
| 679 | } |
||||
| 680 | |||||
| 681 | /** |
||||
| 682 | * @param Request $request |
||||
| 683 | * @param $id |
||||
| 684 | * @return array |
||||
| 685 | */ |
||||
| 686 | public function handleIndexGuestPermission(Request $request, $id) |
||||
| 687 | { |
||||
| 688 | if (isset($request['filters'])) { |
||||
| 689 | $filters = json_decode($request['filters'], true); |
||||
| 690 | } else { |
||||
| 691 | $filters = []; |
||||
| 692 | } |
||||
| 693 | |||||
| 694 | $filters = $this->addFilter($filters, 'owner_id', '=', null); |
||||
| 695 | |||||
| 696 | if (!is_null($this->groupTitle)) { |
||||
| 697 | $filters = $this->addFilter($filters, 'group.title', '=', null); |
||||
| 698 | } |
||||
| 699 | |||||
| 700 | $request['filters'] = json_encode($filters); |
||||
| 701 | |||||
| 702 | return [$request, $filters]; |
||||
| 703 | } |
||||
| 704 | |||||
| 705 | /** |
||||
| 706 | * @param Request $request |
||||
| 707 | * @param $id |
||||
| 708 | * @return array |
||||
| 709 | */ |
||||
| 710 | public function handleStoreAdminPermission(Request $request, $id) |
||||
| 711 | { |
||||
| 712 | $request['group_id'] = null; |
||||
| 713 | |||||
| 714 | $request['owner_id'] = auth()->id(); |
||||
| 715 | |||||
| 716 | return [$request, null]; |
||||
| 717 | } |
||||
| 718 | |||||
| 719 | /** |
||||
| 720 | * @param Request $request |
||||
| 721 | * @param $id |
||||
| 722 | * @return array |
||||
| 723 | */ |
||||
| 724 | public function handleStoreBranchPermission(Request $request, $id) |
||||
| 725 | { |
||||
| 726 | // check for group of user |
||||
| 727 | if (!is_null($this->groupTitle)) { |
||||
| 728 | // get group model |
||||
| 729 | $group = new Group(); |
||||
| 730 | $group = $group->where('title', $this->groupTitle)->first(); |
||||
| 731 | $request['group_id'] = $group['id']; |
||||
| 732 | } |
||||
| 733 | |||||
| 734 | $request['owner_id'] = auth()->id(); |
||||
| 735 | |||||
| 736 | return [$request, null]; |
||||
| 737 | } |
||||
| 738 | |||||
| 739 | /** |
||||
| 740 | * @param Request $request |
||||
| 741 | * @param $id |
||||
| 742 | * @return array |
||||
| 743 | */ |
||||
| 744 | public function handleStoreOwnPermission(Request $request, $id) |
||||
| 745 | { |
||||
| 746 | // check for group of user |
||||
| 747 | if (!is_null($this->groupTitle)) { |
||||
| 748 | // get group model |
||||
| 749 | $group = new Group(); |
||||
| 750 | $group = $group->where('title', $this->groupTitle)->first(); |
||||
| 751 | $request['group_id'] = $group['id']; |
||||
| 752 | } |
||||
| 753 | |||||
| 754 | $request['owner_id'] = auth()->id(); |
||||
| 755 | |||||
| 756 | return [$request, null]; |
||||
| 757 | } |
||||
| 758 | |||||
| 759 | /** |
||||
| 760 | * @param Request $request |
||||
| 761 | * @param $id |
||||
| 762 | * @return array |
||||
| 763 | */ |
||||
| 764 | public function handleStoreGuestPermission(Request $request, $id) |
||||
| 765 | { |
||||
| 766 | $request['owner_id'] = null; |
||||
| 767 | |||||
| 768 | return [$request, null]; |
||||
| 769 | } |
||||
| 770 | |||||
| 771 | public function handleShowAdminPermission(Request $request,$id) |
||||
| 772 | { |
||||
| 773 | $filters = []; |
||||
| 774 | |||||
| 775 | return [$request,$filters]; |
||||
| 776 | } |
||||
| 777 | |||||
| 778 | public function handleShowBranchPermission(Request $request,$id) |
||||
| 779 | { |
||||
| 780 | $filters = []; |
||||
| 781 | array_push($filters, |
||||
| 782 | ["owner_id", "=", auth()->id()] |
||||
| 783 | ); |
||||
| 784 | |||||
| 785 | // check for group of user |
||||
| 786 | if (!is_null($this->groupTitle)) { |
||||
| 787 | |||||
| 788 | // get group model |
||||
| 789 | $group = new Group(); |
||||
| 790 | $group = $group->where('title', $this->groupTitle)->first(); |
||||
| 791 | |||||
| 792 | // put group_id filter |
||||
| 793 | array_push($filters, |
||||
| 794 | ['group_id', '=', $group['id']]); |
||||
| 795 | } |
||||
| 796 | |||||
| 797 | return [$request,$filters]; |
||||
| 798 | } |
||||
| 799 | |||||
| 800 | public function handleShowOwnPermission(Request $request,$id) |
||||
| 801 | { |
||||
| 802 | $filters = []; |
||||
| 803 | array_push($filters, |
||||
| 804 | ["owner_id", "=", auth()->id()] |
||||
| 805 | ); |
||||
| 806 | |||||
| 807 | // check for group of user |
||||
| 808 | if (!is_null($this->groupTitle)) { |
||||
| 809 | |||||
| 810 | // get group model |
||||
| 811 | $group = new Group(); |
||||
| 812 | $group = $group->where('title', $this->groupTitle)->first(); |
||||
| 813 | |||||
| 814 | // put group_id filter |
||||
| 815 | array_push($filters, |
||||
| 816 | ['group_id', '=', $group['id']]); |
||||
| 817 | } |
||||
| 818 | |||||
| 819 | return [$request,$filters]; |
||||
| 820 | } |
||||
| 821 | |||||
| 822 | public function handleShowGuestPermission(Request $request,$id) |
||||
| 823 | { |
||||
| 824 | $filters = []; |
||||
| 825 | array_push($filters, |
||||
| 826 | ["owner_id", "=", auth()->id()] |
||||
| 827 | ); |
||||
| 828 | |||||
| 829 | // check for group of user |
||||
| 830 | if (!is_null($this->groupTitle)) { |
||||
| 831 | |||||
| 832 | // get group model |
||||
| 833 | $group = new Group(); |
||||
| 834 | $group = $group->where('title', $this->groupTitle)->first(); |
||||
| 835 | |||||
| 836 | // put group_id filter |
||||
| 837 | array_push($filters, |
||||
| 838 | ['group_id', '=', $group['id']]); |
||||
| 839 | } |
||||
| 840 | |||||
| 841 | return [$request,$filters]; |
||||
| 842 | } |
||||
| 843 | |||||
| 844 | public function handleOwnPermission($functionName, Request $request = null, $params) |
||||
| 845 | { |
||||
| 846 | switch ($functionName) { |
||||
| 847 | case 'index': |
||||
| 848 | case 'store': |
||||
| 849 | case 'show': |
||||
| 850 | case 'edit': |
||||
| 851 | $filters = []; |
||||
| 852 | array_push($filters, |
||||
| 853 | ["owner_id", "=", auth()->id()] |
||||
| 854 | ); |
||||
| 855 | |||||
| 856 | // check gor group of user |
||||
| 857 | if (!is_null($this->groupTitle)) { |
||||
| 858 | // get group model |
||||
| 859 | $group = new Group(); |
||||
| 860 | $group = $group->where('title', $this->groupTitle)->first(); |
||||
| 861 | |||||
| 862 | // put group_id filter |
||||
| 863 | array_push($filters, |
||||
| 864 | ['group_id', '=', $group['id']]); |
||||
| 865 | } |
||||
| 866 | |||||
| 867 | return $filters; |
||||
| 868 | case 'create': |
||||
| 869 | case 'update': |
||||
| 870 | |||||
| 871 | $filters = []; |
||||
| 872 | // put to filter |
||||
| 873 | array_push($filters, |
||||
| 874 | ["owner_id", "=", auth()->id()] |
||||
| 875 | ); |
||||
| 876 | |||||
| 877 | // put to request |
||||
| 878 | $request['owner_id'] = auth()->id(); |
||||
| 879 | |||||
| 880 | // check for group of user |
||||
| 881 | if (!is_null($this->groupTitle)) { |
||||
| 882 | // get group model |
||||
| 883 | $group = new Group(); |
||||
| 884 | $group = $group->where('title', $this->groupTitle)->first(); |
||||
| 885 | |||||
| 886 | // put group_id filter |
||||
| 887 | array_push($filters, |
||||
| 888 | ['group_id', '=', $group['id']]); |
||||
| 889 | |||||
| 890 | // put to request |
||||
| 891 | $request['group_id'] = $group['id']; |
||||
| 892 | } |
||||
| 893 | |||||
| 894 | $request['permission_filters'] = $filters; |
||||
| 895 | return $request; |
||||
| 896 | case 'destroy': |
||||
| 897 | $filters = []; |
||||
| 898 | array_push($filters, |
||||
| 899 | ["owner_id", "=", auth()->id()], |
||||
| 900 | ["group_id", "=", $this->groupTitle] |
||||
| 901 | ); |
||||
| 902 | return $filters; |
||||
| 903 | default: |
||||
| 904 | return $params; |
||||
| 905 | } |
||||
| 906 | } |
||||
| 907 | |||||
| 908 | // public function handleGuestPermission($functionName, Request $request = null, $params) |
||||
| 909 | // { |
||||
| 910 | // return []; |
||||
| 911 | // switch ($functionName) { |
||||
| 912 | // case 'index': |
||||
| 913 | // case 'store': |
||||
| 914 | // case 'show': |
||||
| 915 | // case 'edit': |
||||
| 916 | // case 'create': |
||||
| 917 | // case 'update': |
||||
| 918 | // case 'destroy': |
||||
| 919 | // default: |
||||
| 920 | // return $params; |
||||
| 921 | // } |
||||
| 922 | // } |
||||
| 923 | |||||
| 924 | // /** |
||||
| 925 | // * @param $functionName |
||||
| 926 | // * @param Request|null $request |
||||
| 927 | // * @param array $params |
||||
| 928 | // * @return array |
||||
| 929 | // */ |
||||
| 930 | // public function handleAdminPermission($functionName, Request $request = null, $params = []) |
||||
| 931 | // { |
||||
| 932 | // return []; |
||||
| 933 | // switch ($functionName) { |
||||
| 934 | // case 'index': |
||||
| 935 | // case 'store': |
||||
| 936 | // case 'show': |
||||
| 937 | // case 'edit': |
||||
| 938 | // case 'create': |
||||
| 939 | // case 'update': |
||||
| 940 | // case 'destroy': |
||||
| 941 | // default: |
||||
| 942 | // return $params; |
||||
| 943 | // } |
||||
| 944 | // } |
||||
| 945 | |||||
| 946 | // public function handleBranchPermission($functionName, Request $request = null, $params) |
||||
| 947 | // { |
||||
| 948 | // return []; |
||||
| 949 | // switch ($functionName) { |
||||
| 950 | // case 'index': |
||||
| 951 | // case 'store': |
||||
| 952 | // case 'show': |
||||
| 953 | // case 'edit': |
||||
| 954 | // case 'create': |
||||
| 955 | // case 'update': |
||||
| 956 | // case 'destroy': |
||||
| 957 | // default: |
||||
| 958 | // return $params; |
||||
| 959 | // } |
||||
| 960 | // } |
||||
| 961 | |||||
| 962 | /** |
||||
| 963 | * @param array $filters |
||||
| 964 | * @param $key |
||||
| 965 | * @param $operator |
||||
| 966 | * @param $value |
||||
| 967 | * @return array |
||||
| 968 | */ |
||||
| 969 | public function addFilter(array $filters, $key, $operator, $value) |
||||
| 970 | { |
||||
| 971 | if (count(explode('.', $key)) == 1 && $this->filedExist($key)) { |
||||
| 972 | array_push($filters, |
||||
| 973 | [ |
||||
| 974 | 'key' => $key, |
||||
| 975 | 'operator' => $operator, |
||||
| 976 | 'value' => $value, |
||||
| 977 | ] |
||||
| 978 | ); |
||||
| 979 | } |
||||
| 980 | return $filters; |
||||
| 981 | } |
||||
| 982 | |||||
| 983 | /** |
||||
| 984 | * @param $key |
||||
| 985 | * @return bool |
||||
| 986 | */ |
||||
| 987 | public function filedExist($key) |
||||
| 988 | { |
||||
| 989 | return in_array($key, $this->model->getFillable()); |
||||
| 990 | } |
||||
| 991 | |||||
| 992 | /** |
||||
| 993 | * @return string |
||||
| 994 | */ |
||||
| 995 | public function getLocalPrefix(): string |
||||
| 996 | { |
||||
| 997 | return $this->localPrefix; |
||||
| 998 | } |
||||
| 999 | |||||
| 1000 | /** |
||||
| 1001 | * @param string $localPrefix |
||||
| 1002 | */ |
||||
| 1003 | public function setLocalPrefix(string $localPrefix) |
||||
| 1004 | { |
||||
| 1005 | $this->localPrefix = $localPrefix; |
||||
| 1006 | } |
||||
| 1007 | } |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths