Files
compras/app/Services/SgfInvoiceGateway.php
T
nalaandClaude Opus 5.5 ec934455e7 Aplicar Pint e formatação de frontend; delimitar o âmbito do lint
Só formatação automática (pint, vp check --fix), sem alterar comportamento.
Excluídos da formatação os registos e artefactos com bytes fixos: docs/,
integrations/, storage/, tmp/, output/, resources/data/, tests/Fixtures/,
compras-release.json e AGENTS*.md. Scripts operacionais, integrations e
auxiliares de teste ficam fora do lint (usam new Function; limpeza na fase 2).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-30 23:01:42 +02:00

61 lines
3.4 KiB
PHP

<?php
namespace App\Services;
use App\Models\User;
use Illuminate\Support\Facades\Http;
use LogicException;
use Tempo\SgcInvoiceClient\InvoiceClient;
/** Prepared gateway only; no event listener, route, scheduled job or automatic transmission. */
final class SgfInvoiceGateway
{
public function clientFor(User $actor, string $connection): InvoiceClient
{
if (config('sgf_invoice.enabled') !== true) {
throw new LogicException('SGF invoice gateway disabled.');
}
$all = config('sgf_invoice.connections', []);
$c = $all[$connection] ?? null;
abort_unless(is_array($c) && ($c['enabled'] ?? false) === true && auth()->id() === $actor->id, 403);
$current = User::where('tenant_id', $actor->tenant_id)->findOrFail($actor->id);
abort_unless(($c['source_scope']['tenant_id'] ?? null) === $current->tenant_id && $current->hasFunctions(['finance'])
&& ! app(OrganizationAccess::class)->maintenanceOnly($current), 403);
$path = config('sgf_invoice.client_package');
$pin = config('sgf_invoice.client_package_sha256');
$actual = is_string($path) && is_file($path) ? hash_file('sha256', $path) : false;
if (! is_string($path) || is_link($path) || ! is_file($path) || ! is_string($pin) || ! preg_match('/^[a-f0-9]{64}$/D', $pin)
|| ! is_string($actual) || ! hash_equals($pin, $actual)) {
throw new LogicException('Verified client package required.');
}
if (class_exists(InvoiceClient::class, false)) {
$loaded = (new \ReflectionClass(InvoiceClient::class))->getFileName();
$loadedHash = is_string($loaded) && is_file($loaded) ? hash_file('sha256', $loaded) : false;
if (! is_string($loadedHash) || ! hash_equals($pin, $loadedHash)) {
throw new LogicException('Client version changed; restart separately.');
}
} else {
require_once $path;
}
$configurationHash = hash('sha256', json_encode($c, JSON_THROW_ON_ERROR));
return new InvoiceClient($c['client_id'], $c['token'], $c['context_version'], $c['source_scope'], $c['target_scope'],
function (string $method, string $url, array $headers, ?array $body) use ($actor, $connection, $configurationHash): array {
// Recheck the originating person's authority immediately before every network operation.
$now = config('sgf_invoice.connections', [])[$connection] ?? null;
if (config('sgf_invoice.enabled') !== true || ! is_array($now) || ! hash_equals($configurationHash, hash('sha256', json_encode($now, JSON_THROW_ON_ERROR)))
|| auth()->id() !== $actor->id) {
return ['status' => 403, 'json' => null];
}
$current = User::where('tenant_id', $actor->tenant_id)->find($actor->id);
if (! $current || ! $current->hasFunctions(['finance']) || app(OrganizationAccess::class)->maintenanceOnly($current)) {
return ['status' => 403, 'json' => null];
}
$http = Http::withHeaders($headers)->acceptJson()->asJson()->withoutRedirecting()->connectTimeout(3)->timeout(15);
$response = $method === 'GET' ? $http->get($url) : $http->post($url, $body);
return ['status' => $response->status(), 'json' => $response->json()];
}, $c['origin']);
}
}