Files
compras/tests/Support/common-receipt-worker.php
T
nalaandClaude Opus 5.5 f590a29e55 Aplicar Pint aos 18 ficheiros que falhavam lint:check
Só formatação. 16 já falhavam em fase5-producao; 2 vêm de c9bd6db.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 17:40:37 +02:00

42 lines
1.9 KiB
PHP

<?php
use App\Models\PurchaseOrder;
use App\Models\User;
use App\Services\ReceiptWorkflow;
use Illuminate\Contracts\Console\Kernel;
use Illuminate\Support\Facades\DB;
use Illuminate\Validation\ValidationException;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
$root = realpath((string) getenv('STOCK_RECEIPT_ROOT'));
$file = realpath($argv[1] ?? '');
if (! $root || ! $file || dirname($file) !== $root) {
throw new RuntimeException('Unexpected private worker');
}
require getcwd().'/vendor/autoload.php';
$app = require getcwd().'/bootstrap/app.php';
$app->make(Kernel::class)->bootstrap();
$db = DB::connection();
$i = $db->selectOne("SELECT current_database() AS name,current_user AS role,current_setting('cluster_name') AS cluster");
if (! preg_match('/^compras_receipts_test_([a-f0-9]{12})$/D', $i->name, $m) || $i->role !== 'stock_receipt_qa_'.$m[1] || $i->cluster !== 'sgf_modular_regression_'.getenv('SGF_ISOLATED_REGRESSION_RUN')) {
throw new RuntimeException('Wrong receipt worker database');
}
$input = json_decode(file_get_contents($file), true, 512, JSON_THROW_ON_ERROR);
$db->select("SELECT set_config('application_name',?,false)", [$input['application']]);
$db->statement("SET lock_timeout='8s'");
$db->statement("SET statement_timeout='12s'");
$actor = User::findOrFail($input['actor']);
auth()->guard()->setUser($actor);
file_put_contents($file.'.ready', 'ready');
$start = hrtime(true);
try {
$receipt = app(ReceiptWorkflow::class)->confirm($actor, PurchaseOrder::findOrFail($input['order']), $input['payload']);
$result = ['status' => 200, 'receipt_id' => $receipt->id];
} catch (ValidationException) {
$result = ['status' => 422];
} catch (HttpExceptionInterface $e) {
$result = ['status' => $e->getStatusCode()];
}
$result['elapsed_ms'] = round((hrtime(true) - $start) / 1e6, 3);
file_put_contents($file.'.result', json_encode($result, JSON_THROW_ON_ERROR));