Lines 100.00% 8 / 8
Functions and Methods 100.00% 3 / 3
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
MapNotAvailableEvent 100.00% 8 / 8 100.00% 3 / 3 4 100.00% 1 / 1
 __construct 100.00% 1 / 1 100.00% 1 / 1 1
 from 100.00% 6 / 6 100.00% 1 / 1 2
 summary 100.00% 1 / 1 100.00% 1 / 1 1
1<?php
2
3declare(strict_types=1);
4
5namespace Lorekeeper\Server\Event\Map;
6
7use Lorekeeper\Core\World\Map\MapName;
8use Lorekeeper\Server\Event\Event;
9use Lorekeeper\Server\Event\NamedAfterClass;
10use Lorekeeper\Server\Event\Notice;
11use Lorekeeper\Server\Message\Message;
12use Override;
13use Psl\Option;
14
15/**
16 * The server refused to move our player because the map is not available.
17 */
18final readonly class MapNotAvailableEvent implements Event
19{
20    use NamedAfterClass;
21
22    public function __construct(
23        public MapName $mapName,
24    ) {}
25
26    /** @return Option\Option<static> */
27    #[Override]
28    public static function from(Message $message): Option\Option
29    {
30        return Notice::text($message, 'warning')->andThen(static function (string $text): Option\Option {
31            $match = [];
32
33            if (preg_match('/^(\S+) is not available/i', $text, $match) !== 1) {
34                return Option\none();
35            }
36
37            return Option\some($match[1]);
38        })->map(static fn(string $name): static => new static(new MapName($name)));
39    }
40
41    #[Override]
42    public function summary(): string
43    {
44        return "{$this->mapName->value} is not available";
45    }
46}