Lines
100.00%
14 / 14
Functions and Methods
100.00%
3 / 3
Classes and Traits
100.00%
1 / 1
| Name | Lines | Functions and Methods | CRAP | Classes and Traits | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Outcome | 100.00% | 14 / 14 | 100.00% | 3 / 3 | 12 | 100.00% | 1 / 1 | |||
| __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 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Lorekeeper\Client\Script; | |
| 6 | ||
| 7 | use Lorekeeper\Server\Event\Event; | |
| 8 | ||
| 9 | final 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 | } |