Skip to content
This repository has been archived by the owner on Jul 8, 2020. It is now read-only.

Commit

Permalink
Use new way of throwing validation exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
mzur committed Apr 20, 2018
1 parent 290a1eb commit d02c4ab
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions src/Http/Controllers/Api/VolumeImageMetadataController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Biigle\Volume;
use Illuminate\Http\Request;
use Biigle\Http\Controllers\Api\Controller;
use Illuminate\Validation\ValidationException;

class VolumeImageMetadataController extends Controller
{
Expand Down Expand Up @@ -76,27 +77,27 @@ public function store(Request $request, $id)
$columns = $csv->fgetcsv();

if (!is_array($columns)) {
return $this->buildFailedValidationResponse($request, [
throw ValidationException::withMessages([
'file' => 'CSV file could not be read or is empty.',
]);
}

if (!in_array('filename', $columns)) {
return $this->buildFailedValidationResponse($request, [
throw ValidationException::withMessages([
'file' => 'The filename column is required.',
]);
}

$colCount = count($columns);

if ($colCount === 1) {
return $this->buildFailedValidationResponse($request, [
throw ValidationException::withMessages([
'file' => 'No metadata columns given.',
]);
}

if ($colCount !== count(array_unique($columns))) {
return $this->buildFailedValidationResponse($request, [
throw ValidationException::withMessages([
'file' => 'Each column may occur only once.',
]);
}
Expand All @@ -105,15 +106,15 @@ public function store(Request $request, $id)
$diff = array_diff($columns, $allowedColumns);

if (count($diff) > 0) {
return $this->buildFailedValidationResponse($request, [
throw ValidationException::withMessages([
'file' => 'The columns array may contain only values of: '.implode(', ', $allowedColumns).'.',
]);
}

$lng = in_array('lng', $columns);
$lat = in_array('lat', $columns);
if ($lng && !$lat || !$lng && $lat) {
return $this->buildFailedValidationResponse($request, [
throw ValidationException::withMessages([
'file' => "If the 'lng' column is present, the 'lat' column must be present, too (and vice versa).",
]);
}
Expand All @@ -122,7 +123,7 @@ public function store(Request $request, $id)

// isset($data[0]) skips a possible empty last line which returns [0 => null]
if (!(is_array($data) && isset($data[0]))) {
return $this->buildFailedValidationResponse($request, [
throw ValidationException::withMessages([
'file' => 'The CSV file has no data rows.',
]);
}
Expand All @@ -135,7 +136,7 @@ public function store(Request $request, $id)
// Read all rows of the CSV and update the image models.
while (is_array($data) && isset($data[0])) {
if (count($data) !== $colCount) {
return $this->buildFailedValidationResponse($request, [
throw ValidationException::withMessages([
'file' => 'Column count in the CSV file does not match the given columns: '.implode(', ', $columns).'.',
]);
}
Expand All @@ -144,7 +145,7 @@ public function store(Request $request, $id)
$filename = $toFill['filename'];

if (!$images->has($filename)) {
return $this->buildFailedValidationResponse($request, [
throw ValidationException::withMessages([
'file' => "There is no image with filename {$filename}.",
]);
}
Expand All @@ -155,9 +156,7 @@ public function store(Request $request, $id)
$this->fillImageAttributes($image, $toFill);
$this->fillImageMetadata($image, $toFill);
} catch (Exception $e) {
return $this->buildFailedValidationResponse($request, [
'file' => $e->getMessage(),
]);
throw ValidationException::withMessages(['file' => $e->getMessage()]);
}

$data = $csv->fgetcsv();
Expand Down

0 comments on commit d02c4ab

Please sign in to comment.