@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use LogicException;
|
||||
use Tempo\Finance\Budget\BudgetContext;
|
||||
use Tempo\Finance\OrderCancellation\CommitmentReversal;
|
||||
|
||||
/** Version selection for the existing additive reversal writer, never another budget ledger. */
|
||||
final class OrderCancellationRuntime
|
||||
{
|
||||
public const MANIFEST_SHA256 = '52df7927aa2689598ce1be92de1483694347a696245704abb9e3925db7abfc0f';
|
||||
|
||||
public function assertReady(): void
|
||||
{
|
||||
$runtime = app(StandaloneRuntime::class);
|
||||
if (! $runtime->enabled()) {
|
||||
return;
|
||||
}
|
||||
$runtime->assertInstallation(DB::connection());
|
||||
$runtime->assertLoadedPackages();
|
||||
if (! property_exists(BudgetContext::class, 'requisitionCycles')) {
|
||||
throw new LogicException('COMPRAS_CANCELLATION_CYCLE_CONTEXT');
|
||||
}
|
||||
foreach (['order_cancellations' => ['operation_key', 'basis_hash', 'status'],
|
||||
'order_cancellation_policies' => ['revision', 'status'], 'order_cancellation_policy_reviews' => ['reviewed_by'],
|
||||
'budget_entries' => ['requisition_cycle'], 'budget_requisition_cycles' => ['cycle', 'state']] as $table => $columns) {
|
||||
if (! DB::getSchemaBuilder()->hasColumns($table, $columns)) {
|
||||
throw new LogicException('COMPRAS_CANCELLATION_SCHEMA');
|
||||
}
|
||||
}
|
||||
$root = config('order_cancellation.root');
|
||||
if (! is_string($root)) {
|
||||
throw new LogicException('COMPRAS_CANCELLATION_ROOT');
|
||||
}
|
||||
self::verifyArtifact($root);
|
||||
}
|
||||
|
||||
public static function verifyArtifact(string $root): void
|
||||
{
|
||||
$bytes = is_file($root.'/manifest.json') ? file_get_contents($root.'/manifest.json') : false;
|
||||
$path = $root.'/src/CommitmentReversal.php';
|
||||
$hash = is_file($path) ? hash_file('sha256', $path) : false;
|
||||
if (! is_string($bytes) || ! hash_equals(self::MANIFEST_SHA256, hash('sha256', $bytes))
|
||||
|| is_link($root) || is_link($root.'/manifest.json') || is_link($path)
|
||||
|| ! str_starts_with(realpath($path) ?: '', (realpath($root) ?: '').DIRECTORY_SEPARATOR)) {
|
||||
throw new LogicException('COMPRAS_CANCELLATION_MANIFEST');
|
||||
}
|
||||
$manifest = json_decode($bytes, true, 32, JSON_THROW_ON_ERROR);
|
||||
if (! is_string($hash) || ! hash_equals($manifest['files']['src/CommitmentReversal.php'], $hash)) {
|
||||
throw new LogicException('COMPRAS_CANCELLATION_FILE');
|
||||
}
|
||||
}
|
||||
|
||||
/** Returns whether the writer requires the explicit cycle map in its financial context. */
|
||||
public function load(int $origin): bool
|
||||
{
|
||||
$operational = app(StandaloneRuntime::class)->enabled();
|
||||
if ($operational) {
|
||||
$this->assertReady();
|
||||
$path = config('order_cancellation.root').'/src/CommitmentReversal.php';
|
||||
} else {
|
||||
abort_if(DB::getSchemaBuilder()->hasColumn('budget_entries', 'requisition_cycle')
|
||||
&& DB::table('budget_entries')->where('requisition_id', $origin)->where('requisition_cycle', '>', 1)->exists(), 503,
|
||||
'Preserve o componente compatível com os ciclos antes de cancelar esta ordem.');
|
||||
$path = base_path('packages/order-cancellation/src/CommitmentReversal.php');
|
||||
abort_unless(is_file($path) && hash_file('sha256', $path) === config('order_cancellation.reversal_sha256'), 503, 'Capacidade de reversão indisponível.');
|
||||
}
|
||||
if (class_exists(CommitmentReversal::class, false)
|
||||
&& realpath((new \ReflectionClass(CommitmentReversal::class))->getFileName() ?: '') !== realpath($path)) {
|
||||
throw new LogicException('COMPRAS_CANCELLATION_CLASS_LOCATION');
|
||||
}
|
||||
require_once $path;
|
||||
|
||||
return $operational;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user