41 lines
1.6 KiB
PHP
41 lines
1.6 KiB
PHP
<?php
|
|
|
|
use App\Models\QualificationSet;
|
|
use App\Models\Requisition;
|
|
use App\Models\User;
|
|
use App\Services\DocumentaryQualificationWorkflow;
|
|
use Illuminate\Contracts\Console\Kernel;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Validation\ValidationException;
|
|
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
|
|
|
|
require __DIR__.'/../../vendor/autoload.php';
|
|
$database = $argv[1] ?? '';
|
|
if (! preg_match('/^compras_receipts_test_[a-f0-9]{12}$/', $database)) {
|
|
throw new LogicException('Base de teste inválida.');
|
|
}
|
|
$app = require __DIR__.'/../../bootstrap/app.php';
|
|
$app->make(Kernel::class)->bootstrap();
|
|
if (! $app->environment('testing') || DB::connection()->getDatabaseName() !== $database) {
|
|
throw new LogicException('Worker não isolado.');
|
|
}
|
|
DB::select('select set_config(?, ?, false)', ['application_name', 'sgc_qualification_'.$argv[4]]);
|
|
$pid = DB::selectOne('select pg_backend_pid() as pid')->pid;
|
|
$actor = User::query()->findOrFail($argv[2]);
|
|
auth()->guard()->setUser($actor);
|
|
$payload = json_decode(stream_get_contents(STDIN), true, 512, JSON_THROW_ON_ERROR);
|
|
try {
|
|
$workflow = app(DocumentaryQualificationWorkflow::class);
|
|
if ($payload['action'] === 'define') {
|
|
$workflow->define($actor, Requisition::query()->findOrFail($argv[3]), $payload['data']);
|
|
} else {
|
|
$workflow->assess($actor, QualificationSet::query()->findOrFail($argv[3]), $payload['data']);
|
|
}
|
|
$status = 200;
|
|
} catch (ValidationException) {
|
|
$status = 422;
|
|
} catch (HttpExceptionInterface $exception) {
|
|
$status = $exception->getStatusCode();
|
|
}
|
|
echo json_encode(['status' => $status, 'backend_pid' => $pid], JSON_THROW_ON_ERROR);
|