Last active
December 6, 2018 08:30
-
-
Save thiagomarini/b55d9ae7c8ec803e9cc8061905665258 to your computer and use it in GitHub Desktop.
Laravel Spaghetti Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Create new shipment barcode | |
* | |
* $param Request $request | |
* @return void | |
**/ | |
public function createShipmentBarcode(Request $request) | |
{ | |
// validate required fields | |
$validator = Validator::make($request->json()->all(), [ | |
'barcode' => 'required|string|unique:shipment_barcode,barcode', | |
'shipment_id' => 'required|integer|exists:shipment,id', | |
]); | |
if ($validator->fails()) { | |
if (!$request->isJson()) { | |
return response([ | |
'result' => 'ERROR', | |
'code' => 400, | |
'error' => 'Invalid JSON recieved', | |
'display' => 'Invalid JSON recieved', | |
'error_code' => 1 | |
], 400); | |
} | |
if ($validator->errors()->first() != '') { | |
return response([ | |
'result' => 'ERROR', | |
'code' => 400, | |
'error' => $validator->errors()->first(), | |
'display' => $validator->errors()->first(), | |
'error_code' => 2 | |
], 400); | |
} | |
} | |
// get shipment in order to perform checks | |
$shipment = Shipment::find($request->shipment_id); | |
// if shipment doesn't belong to user, return an error | |
if ($shipment->user_id != $request->user->id) { | |
return response([ | |
'result' => 'ERROR', | |
'code' => 400, | |
'error' => 'This shipment does not belong to this user', | |
'display' => 'This shipment does not belong to this user', | |
'error_code' => 3 | |
], 400); | |
} | |
// if shipment is already picked up, cannot assign barcode | |
if (! in_array($shipment->status, ['Created', 'Summoned', 'PickedUp'])) { | |
return response([ | |
'result' => 'ERROR', | |
'code' => 400, | |
'error' => 'Cannot assign barcode on picked up shipment', | |
'display' => 'Cannot assign barcode on picked up shipment', | |
'error_code' => 4 | |
], 400); | |
} | |
// Create new shipment barcode | |
$shipmentBarcode = new ShipmentBarcode; | |
$shipmentBarcode->barcode = $request->barcode; | |
$shipmentBarcode->shipment_id = $request->shipment_id; | |
$shipmentBarcode->barcode_type = 'WEB'; | |
$shipmentBarcode->created_at = date('Y-m-d H:i:s'); | |
$shipmentBarcode->save(); | |
//Create instructions for JSON response | |
$responseDetailInstructions = new ResponseDetailInstructions([ | |
'detailLevel' => ResponseDetailInstructions::DETAIL_LEVEL_MAXIMUM, | |
]); | |
// return JSON response | |
return response()->json([ | |
'result' => 'OK', | |
'shipment_barcode' => $shipmentBarcode->propertiesForResponse($responseDetailInstructions) | |
], 200, [], JSON_UNESCAPED_SLASHES); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment