InvalidStateTransitionException.php

Code Hygiene Score: 100

Keine Issues gefunden.

Dependencies 4

Klassen 1

Funktionen 2

Versionen 1

Code

<?php

declare(strict_types=1);

namespace Domain\Exception;

// @responsibility: Exception bei ungültigem Status-Übergang

use BackedEnum;
use DomainException;

final class InvalidStateTransitionException extends DomainException
{
    /**
     * @param BackedEnum $from Current status
     * @param BackedEnum $to Target status
     * @param array<BackedEnum> $allowed Allowed transitions from current status
     */
    public function __construct(
        public readonly BackedEnum $from,
        public readonly BackedEnum $to,
        public readonly array $allowed
    ) {
        $allowedValues = array_map(
            static fn (BackedEnum $s): string => (string) $s->value,
            $allowed
        );

        $message = sprintf(
            'Ungültiger Statusübergang von "%s" nach "%s". Erlaubt: %s',
            $from->value,
            $to->value,
            $allowedValues !== [] ? implode(', ', $allowedValues) : 'keine'
        );

        parent::__construct($message);
    }

    /**
     * Get allowed transition values as strings.
     *
     * @return array<string>
     */
    public function getAllowedValues(): array
    {
        return array_map(
            static fn (BackedEnum $s): string => (string) $s->value,
            $this->allowed
        );
    }
}
← Übersicht Graph