Backup #1812

ID1812
Dateipfad/var/www/dev.campus.systemische-tools.de/src/Domain/ValueObject/ExecutionDuration.php
Version1
Typ modified
Größe2.4 KB
Hash80cd41c556cc509247073ed752a5f2bf2b6bff985dc00b7baf8734db1efbfe56
Datum2025-12-27 15:38:32
Geändert vonclaude-code-hook
GrundClaude Code Pre-Hook Backup vor Edit-Operation
Datei existiert Ja

Dateiinhalt

<?php

declare(strict_types=1);

namespace Domain\ValueObject;

// @responsibility: Immutables Value Object für Ausführungsdauer mit Zeitstempeln

use InvalidArgumentException;

final class ExecutionDuration
{
    private \DateTimeImmutable $requestedAt;
    private ?\DateTimeImmutable $respondedAt;

    private function __construct(\DateTimeImmutable $requestedAt, ?\DateTimeImmutable $respondedAt)
    {
        if ($respondedAt !== null && $respondedAt < $requestedAt) {
            throw new InvalidArgumentException('Response timestamp cannot be before request timestamp');
        }

        $this->requestedAt = $requestedAt;
        $this->respondedAt = $respondedAt;
    }

    public static function start(\DateTimeImmutable $requestedAt): self
    {
        return new self($requestedAt, null);
    }

    public static function completed(\DateTimeImmutable $requestedAt, \DateTimeImmutable $respondedAt): self
    {
        return new self($requestedAt, $respondedAt);
    }

    public static function now(): self
    {
        return new self(new \DateTimeImmutable(), null);
    }

    public function complete(\DateTimeImmutable $respondedAt): self
    {
        return new self($this->requestedAt, $respondedAt);
    }

    public function completeNow(): self
    {
        return new self($this->requestedAt, new \DateTimeImmutable());
    }

    public function requestedAt(): \DateTimeImmutable
    {
        return $this->requestedAt;
    }

    public function respondedAt(): ?\DateTimeImmutable
    {
        return $this->respondedAt;
    }

    public function durationMs(): ?int
    {
        if ($this->respondedAt === null) {
            return null;
        }

        $diff = $this->respondedAt->getTimestamp() - $this->requestedAt->getTimestamp();

        return $diff * 1000;
    }

    public function isCompleted(): bool
    {
        return $this->respondedAt !== null;
    }

    public function format(): string
    {
        $duration = $this->durationMs();

        if ($duration === null) {
            return 'In progress';
        }

        if ($duration < 1000) {
            return "{$duration}ms";
        }

        return sprintf('%.2fs', $duration / 1000);
    }

    public function toArray(): array
    {
        return [
            'requested_at' => $this->requestedAt->format('Y-m-d H:i:s.u'),
            'responded_at' => $this->respondedAt?->format('Y-m-d H:i:s.u'),
            'duration_ms' => $this->durationMs(),
        ];
    }
}

Vollständig herunterladen

Aktionen

Herunterladen

Andere Versionen dieser Datei

ID Version Typ Größe Datum
1814 3 modified 2.5 KB 2025-12-27 15:38
1813 2 modified 2.5 KB 2025-12-27 15:38
1812 1 modified 2.4 KB 2025-12-27 15:38

← Zurück zur Übersicht