Skip to content

Commit

Permalink
refs #31: Validation failure handling
Browse files Browse the repository at this point in the history
  • Loading branch information
eFrane committed Nov 24, 2017
1 parent 95416a8 commit d2f1ada
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 22 deletions.
16 changes: 16 additions & 0 deletions lib/Spec/Exception/ValidationFailed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
/**
* @copyright 2017
* @author Stefan "eFrane" Graupner <[email protected]>
*/

namespace OParl\Spec\Exception;


class ValidationFailed extends \RuntimeException
{
public static function validatorQuitUnexpectedly()
{
return new self("Validator quit unexpectedly.");
}
}
57 changes: 35 additions & 22 deletions lib/Spec/Jobs/ValidatorRunJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Contracts\Logging\Log;
use Illuminate\Mail\Mailer;
use OParl\Spec\Exception\ValidationFailed;
use Symfony\Component\Process\Process;

class ValidatorRunJob extends Job
Expand Down Expand Up @@ -47,30 +48,35 @@ public function handle(Log $log, Filesystem $fs, Mailer $mailer)
{
$log->info("Beginning Validation for {$this->endpoint}");

$json = $this->runValidator($log, $fs);
try {
$json = $this->runValidator($log, $fs);

$this->saveResult($json);
$this->saveResult($json);

// TODO: l10n/i18n for validation results
Carbon::setLocale('de');
// TODO: l10n/i18n for validation results
Carbon::setLocale('de');

$title = sprintf(
'OParl Validierung am %s für %s',
Carbon::now()->format('d.m.Y'),
$this->endpoint
);
$title = sprintf(
'OParl Validierung am %s für %s',
Carbon::now()->format('d.m.Y'),
$this->endpoint
);

$data = [
'endpoint' => $this->endpoint,
'urlEncodedEndpoint' => urlencode($this->endpoint),
'result' => $json,
'validationDate' => Carbon::now()->format('d.m.Y'),
'title' => $title,
];
$data = [
'endpoint' => $this->endpoint,
'urlEncodedEndpoint' => urlencode($this->endpoint),
'result' => $json,
'validationDate' => Carbon::now()->format('d.m.Y'),
'title' => $title,
];

$log->info("Finished Validation for {$this->endpoint}");
$log->info("Finished Validation for {$this->endpoint}");

$mailer->to($this->email)->send(new ValidationCompleted($data));
$mailer->to($this->email)->send(new ValidationCompleted($data));
} catch (ValidationFailed $e) {
// TODO: proper failure handling including an informative mail
$log->error("Failed validation for {$this->endpoint}");
}
}

/**
Expand All @@ -87,7 +93,7 @@ protected function runValidator(Log $log, Filesystem $fs)
$fs->makeDirectory('validation');
}

$validationResultFile = storage_path('app/validation/'.uniqid('validation-').'.json');
$validationResultFile = storage_path('app/validation/'.uniqid('validation-').'.result');
$validatorCmd = sprintf('./validate --porcelain -fjson -o%s "%s"', $validationResultFile, $this->endpoint);

$validator = new Process($validatorCmd);
Expand All @@ -107,10 +113,17 @@ protected function runValidator(Log $log, Filesystem $fs)
$log->debug($validatorLogPrefix.$data);
});

$json = (array) json_decode(file_get_contents($validationResultFile), true);
unlink($validationResultFile);
if ($validator->getExitCode() !== 0) {
throw ValidationFailed::validatorQuitUnexpectedly();
}

$result = (array) json_decode(file_get_contents($validationResultFile), true);

if (file_exists($validationResultFile)) {
unlink($validationResultFile);
}

return $json;
return $result;
}

/**
Expand Down

0 comments on commit d2f1ada

Please sign in to comment.