Skip to content

Commit

Permalink
Check that model methods do not return falsable
Browse files Browse the repository at this point in the history
The model being updated is now passed as the second parameter to `beforeUpdate`
  • Loading branch information
ezra-obiwale committed Jan 29, 2018
1 parent 0a56afe commit ad41b03
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 3.2.0

- Check that model methods do not return falsable
- The model being updated is now passed as the second parameter to `beforeUpdate`

## 3.1.0

`Laraquick\Models\Traits\Helper` now has an `except` scope method to remove provided
Expand Down
25 changes: 24 additions & 1 deletion src/Controllers/Traits/Crud/Destroy.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ trait Destroy
* @return Response
*/
abstract protected function notFoundError();

/**
* Create a model not set error response
*
* @return Response
*/
abstract protected function modelNotSetError();

/**
* Called when a delete action fails
Expand Down Expand Up @@ -69,6 +76,10 @@ protected function rollbackDestroy()
public function destroy($id)
{
$model = $this->destroyModel();
if (!$model) {
logger()->error('Destroy model undefined');
return $this->modelNotSetError();
}
$item = is_object($model)
? $model->find($id)
: $model::find($id);
Expand Down Expand Up @@ -142,7 +153,11 @@ protected function rollbackDestroyMany()
*/
public function destroyMany(Request $request)
{
$model = $this->model();
$model = $this->destroyModel();
if (!$model) {
logger()->error('Destroy model undefined');
return $this->modelNotSetError();
}
$data = $request->all();
if (!array_key_exists('ids', $data)) {
throw new \Exception('Ids not found');
Expand Down Expand Up @@ -214,6 +229,10 @@ protected function rollbackForceDestroy()
public function forceDestroy($id)
{
$model = $this->destroyModel();
if (!$model) {
logger()->error('Destroy model undefined');
return $this->modelNotSetError();
}
$item = is_object($model)
? $model->find($id)
: $model::find($id);
Expand Down Expand Up @@ -281,6 +300,10 @@ protected function rollbackRestoreDestroyed()
public function restoreDestroyed($id)
{
$model = $this->destroyModel();
if (!$model) {
logger()->error('Destroy model undefined');
return $this->modelNotSetError();
}
$item = is_object($model)
? $model->find($id)
: $model::find($id);
Expand Down
25 changes: 25 additions & 0 deletions src/Controllers/Traits/Crud/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
*/
trait Index
{

/**
* Create a model not set error response
*
* @return Response
*/
abstract protected function modelNotSetError();
/**
* The model to use in the index method.
*
Expand Down Expand Up @@ -90,10 +97,19 @@ public function index()
{
if ($query = request($this->searchQueryParam())) {
$model = $this->searchModel($query);
if (!$model) {
logger()->error('Search model undefined');
}
}
else {
$model = $this->indexModel();
if (!$model) {
logger()->error('Index model undefined');
}
}
if (!$model) {
return $this->modelNotSetError();
}

if ($sorter = request($this->sortParam())) {
$model = $this->sort($sorter, $model);
Expand Down Expand Up @@ -140,10 +156,19 @@ public function trashedIndex()
{
if ($query = request($this->searchQueryParam())) {
$model = $this->searchModel($query);
if (!$model) {
logger()->error('Search model undefined');
}
}
else {
$model = $this->indexModel();
if (!$model) {
logger()->error('Index model undefined');
}
}
if (!$model) {
return $this->modelNotSetError();
}

if ($sorter = request($this->sortParam())) {
$model = $this->sort($sorter, $model);
Expand Down
11 changes: 11 additions & 0 deletions src/Controllers/Traits/Crud/Show.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ trait Show
* @return Response
*/
abstract protected function notFoundError();

/**
* Create a model not set error response
*
* @return Response
*/
abstract protected function modelNotSetError();

/**
* The model to use in the show method.
Expand All @@ -33,6 +40,10 @@ abstract protected function showModel();
public function show($id)
{
$model = $this->showModel();
if (!$model) {
logger()->error('Show model undefined');
return $this->modelNotSetError();
}
$item = is_object($model)
? $model->find($id)
: $model::find($id);
Expand Down
11 changes: 11 additions & 0 deletions src/Controllers/Traits/Crud/Store.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
*/
trait Store
{

/**
* Create a model not set error response
*
* @return Response
*/
abstract protected function modelNotSetError();

/**
* The model to use in the store method.
Expand Down Expand Up @@ -60,6 +67,10 @@ public function store(Request $request)
return $resp;

$model = $this->storeModel();
if (!$model) {
logger()->error('Store model undefined');
return $this->modelNotSetError();
}

try {
DB::beginTransaction();
Expand Down
7 changes: 4 additions & 3 deletions src/Controllers/Traits/Crud/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ abstract protected function updateModel();
/**
* Called after validation but before update method is called
*
* @param array $data
* @param array $data The data to update the model with
* @param Model $model The model to be updated
* @return mixed The response to send or null
*/
protected function beforeUpdate(array &$data)
protected function beforeUpdate(array &$data, Model $model)
{
}

Expand Down Expand Up @@ -76,7 +77,7 @@ public function update(Request $request, $id)

try {
DB::beginTransaction();
if ($resp = $this->beforeUpdate($data)) return $resp;
if ($resp = $this->beforeUpdate($data, $item)) return $resp;

$result = $item->update($data);

Expand Down
10 changes: 10 additions & 0 deletions src/Controllers/Traits/Respond.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,4 +104,14 @@ protected function notFoundError()
{
return $this->error('Resource not found', null, 404);
}

/**
* Create a model not set error response
*
* @return Response
*/
protected function modelNotSetError()
{
return $this->error('Model not set for action', null, 500);
}
}

0 comments on commit ad41b03

Please sign in to comment.