Files
WMS/Modules/M3/Services/PostgresProgramBridge.php
T
2026-09-21 12:35:52 +01:00

104 lines
3.5 KiB
PHP
Executable File

<?php
namespace Modules\M3\Services;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Schema;
/**
* PostgreSQL replacement for the old IBM i Toolkit program-call transport.
*
* The legacy M3 code invokes IBM i programs after writing PostgreSQL-backed
* staging data. Core no longer connects to DB2. These requests are persisted
* so a PostgreSQL-native worker/integration can process them without making
* the web request depend on IBM i drivers.
*/
class PostgresProgramBridge
{
private string $errorCode = '';
private string $errorMessage = '';
private array $options = [];
public function setOptions(array $options): self
{
$this->options = $options;
return $this;
}
public function addParameterChar(string $io, int $size, string $comment = '', string $name = '', mixed $value = null): array
{
return compact('io', 'size', 'comment', 'name', 'value');
}
public function addParameterPackDec(string $io, int $length, int $scale, string $comment = '', string $name = '', mixed $value = null): array
{
return ['type' => 'packed_decimal'] + compact('io', 'length', 'scale', 'comment', 'name', 'value');
}
public function addParameterZoned(string $io, int $length, int $scale, string $comment = '', string $name = '', mixed $value = null): array
{
return ['type' => 'zoned_decimal'] + compact('io', 'length', 'scale', 'comment', 'name', 'value');
}
public function pgmCall(string $program, ?string $library = null, array $parameters = [], array $returnParameters = [], array $options = []): array|false
{
return $this->queue('program', $program, $library, [
'parameters' => $parameters,
'return_parameters' => $returnParameters,
'options' => $options,
]);
}
public function clCommand(string $command): array|false
{
return $this->queue('command', $command, null, []);
}
public function getErrorCode(): string
{
return $this->errorCode;
}
public function getErrorMsg(): string
{
return $this->errorMessage;
}
private function queue(string $type, string $operation, ?string $library, array $payload): array|false
{
try {
$record = [
'type' => $type,
'operation' => $operation,
'library' => $library,
'payload' => json_encode($payload, JSON_THROW_ON_ERROR),
'requested_by' => function_exists('m3_user') ? m3_user() : null,
'status' => 'pending',
'created_at' => now(),
'updated_at' => now(),
];
if (Schema::connection(config('m3.connection', 'pgsql'))->hasTable('m3_program_requests')) {
$id = DB::connection(config('m3.connection', 'pgsql'))
->table('m3_program_requests')
->insertGetId($record);
} else {
Log::warning('M3 PostgreSQL program bridge table is not migrated; request logged only.', $record);
$id = null;
}
return ['success' => true, 'queued' => true, 'request_id' => $id, 'io_param' => []];
} catch (\Throwable $e) {
$this->errorCode = (string) $e->getCode();
$this->errorMessage = $e->getMessage();
Log::error('Unable to queue M3 program request.', ['exception' => $e]);
return false;
}
}
}