Lines 100.00% 14 / 14
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% 9 / 9 100.00% 1 / 1 6
9final readonly class Outcome
10{
11    public function __construct(
12        public OutcomeStatus $status,
13        public string $summary,
14    ) {}
15
16    public static function of(mixed $result): self
17    {
18        return match (true) {
19            $result instanceof Done => new self(OutcomeStatus::Done, $result->summary),
20            $result instanceof Disconnected => new self(OutcomeStatus::Disconnected, $result->reason),
21            $result instanceof Stopped => new self(OutcomeStatus::Cancelled, 'cancelled'),
22            default => new self(OutcomeStatus::Failed, self::reason($result)),
23        };
24    }
25
26    private static function reason(mixed $result): string
27    {
28        return match (true) {
29            is_string($result) => $result,
30            $result instanceof Failed => $result->reason,
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}