agent/php/Agentic/Data/CreditTransaction.php
Snider 470ce0de99 feat(agentic): implement §9 Services (FleetService + CreditService + SessionService) (#849)
Additive-only — no existing files modified.

- FleetService: wraps fleet actions+models, register/heartbeat/dispatch
  (direct or queued), node health snapshots, typed fleet stats
- CreditService: workspace-level balance/refund/deduct/ledger over
  credit_entries, returns typed CreditTransaction DTOs
- SessionService: RFC-§7 lifecycle session creation + guarded state
  transitions + SSE-style emission via Laravel events

DTOs: FleetStats, CreditTransaction (readonly).
Pest Feature tests _Good/_Bad/_Ugly per AX-10. pest skipped (vendor missing).

Co-authored-by: Codex <noreply@openai.com>
Closes tasks.lthn.sh/view.php?id=849
2026-04-25 05:28:49 +01:00

62 lines
1.9 KiB
PHP

<?php
// SPDX-License-Identifier: EUPL-1.2
declare(strict_types=1);
namespace Core\Mod\Agentic\Data;
use Carbon\CarbonImmutable;
use Carbon\CarbonInterface;
final readonly class CreditTransaction
{
public function __construct(
public ?int $id,
public int $workspaceId,
public ?int $fleetNodeId,
public string $taskType,
public int $amount,
public int $balanceAfter,
public ?string $description,
public CarbonImmutable $createdAt,
) {}
public static function fromModel(object $entry): self
{
$createdAt = $entry->created_at ?? null;
if ($createdAt instanceof CarbonImmutable) {
$immutable = $createdAt;
} elseif ($createdAt instanceof CarbonInterface) {
$immutable = CarbonImmutable::instance($createdAt);
} else {
$immutable = CarbonImmutable::parse((string) ($createdAt ?? 'now'));
}
return new self(
id: isset($entry->id) ? (int) $entry->id : null,
workspaceId: (int) ($entry->workspace_id ?? 0),
fleetNodeId: isset($entry->fleet_node_id) ? (int) $entry->fleet_node_id : null,
taskType: (string) ($entry->task_type ?? ''),
amount: (int) ($entry->amount ?? 0),
balanceAfter: (int) ($entry->balance_after ?? 0),
description: isset($entry->description) ? (string) $entry->description : null,
createdAt: $immutable,
);
}
public function toArray(): array
{
return [
'id' => $this->id,
'workspace_id' => $this->workspaceId,
'fleet_node_id' => $this->fleetNodeId,
'task_type' => $this->taskType,
'amount' => $this->amount,
'balance_after' => $this->balanceAfter,
'description' => $this->description,
'created_at' => $this->createdAt->toIso8601String(),
];
}
}