Lines
100.00%
13 / 13
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% | 13 / 13 | 100.00% | 3 / 3 | 11 | 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% | 8 / 8 | 100.00% | 1 / 1 | 5 | |||||
| 1 | <?php | |
| 2 | ||
| 3 | declare(strict_types=1); | |
| 4 | ||
| 5 | namespace Lorekeeper\Client\Script; | |
| 6 | ||
| 7 | use Lorekeeper\Client\Bot\Done; | |
| 8 | use Lorekeeper\Server\Event\Event; | |
| 9 | ||
| 10 | final 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 | } |