Lines 100.00% 13 / 13
Methods 100.00% 3 / 3
Classes 100.00% 1 / 1
Name Lines Methods CRAP
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 of 100.00% 4 / 4 100.00% 1 / 1 5
 reason 100.00% 8 / 8 100.00% 1 / 1 5
10final readonly class Outcome
11{
12    public function __construct(
13        public OutcomeStatus $status,
14        public string $summary,
15    ) {}
16
17    public static function of(mixed $result): self
18    {
19        return match (true) {
20            $result instanceof Done => new self(OutcomeStatus::Done, $result->summary),
21            $result instanceof Disconnected => new self(OutcomeStatus::Disconnected, $result->reason),
22            $result instanceof Stopped => new self(OutcomeStatus::Cancelled, 'cancelled'),
23            default => new self(OutcomeStatus::Failed, self::reason($result)),
24        };
25    }
26
27    private static function reason(mixed $result): string
28    {
29        return match (true) {
30            is_string($result) => $result,
31            $result instanceof TimedOut => 'no answer while waiting for '
32                . implode(', ', array_map(static fn(string $event): string => substr(
33                    $event,
34                    (int) strrpos($event, needle: '\\') + 1,
35                ), $result->await->events)),
36            $result instanceof Event => $result->summary(),
37            default => 'the script ended',
38        };
39    }
40}