MessageTiming.php

Code Hygiene Score: 100

Keine Issues gefunden.

Dependencies 2

Klassen 1

Funktionen 10

Verwendet von 3

Versionen 2

Code

<?php

declare(strict_types=1);

namespace Domain\ValueObject;

// @responsibility: Immutable Value Object for message timing (performance tracking)

use Domain\Constants;
use InvalidArgumentException;

final class MessageTiming
{
    private ?float $startMicrotime;
    private ?float $endMicrotime;

    private function __construct(?float $startMicrotime, ?float $endMicrotime)
    {
        if ($startMicrotime !== null && $endMicrotime !== null && $endMicrotime < $startMicrotime) {
            throw new InvalidArgumentException('End time cannot be before start time');
        }

        $this->startMicrotime = $startMicrotime;
        $this->endMicrotime = $endMicrotime;
    }

    public static function create(?float $startMicrotime, ?float $endMicrotime): self
    {
        return new self($startMicrotime, $endMicrotime);
    }

    public static function none(): self
    {
        return new self(null, null);
    }

    public static function started(float $startMicrotime): self
    {
        return new self($startMicrotime, null);
    }

    public function startMicrotime(): ?float
    {
        return $this->startMicrotime;
    }

    public function endMicrotime(): ?float
    {
        return $this->endMicrotime;
    }

    public function durationMs(): ?float
    {
        if ($this->startMicrotime === null || $this->endMicrotime === null) {
            return null;
        }

        return ($this->endMicrotime - $this->startMicrotime) * Constants::MS_PER_SECOND;
    }

    public function isComplete(): bool
    {
        return $this->startMicrotime !== null && $this->endMicrotime !== null;
    }

    public function withEnd(float $endMicrotime): self
    {
        return new self($this->startMicrotime, $endMicrotime);
    }

    public function equals(self $other): bool
    {
        return $this->startMicrotime === $other->startMicrotime
            && $this->endMicrotime === $other->endMicrotime;
    }
}
← Übersicht Graph