Passed
Push — master ( e1d27c...888025 )
by Paweł
03:21
created

VisitEvent   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Test Coverage

Coverage 22.22%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 16
c 1
b 0
f 0
dl 0
loc 36
ccs 4
cts 18
cp 0.2222
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A __invoke() 0 23 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AardsGerds\Game\Event;
6
7
use AardsGerds\Game\Location\Location;
8
use AardsGerds\Game\Location\Visitor;
9
use AardsGerds\Game\Player\Player;
10
use AardsGerds\Game\Player\PlayerAction;
11
use function Lambdish\Phunctional\map;
12
13
abstract class VisitEvent extends Event
14
{
15 1
    public function __construct(
16
        Context $context,
17
        DecisionCollection $decisionCollection,
18
        protected Location $location,
19
    ) {
20 1
        parent::__construct(
21 1
            $context,
22
            $decisionCollection,
23
        );
24 1
    }
25
26
    public function __invoke(Player $player, PlayerAction $playerAction): Decision
27
    {
28
        $player->setCheckpoint($this);
29
        $playerAction->savePlayerState($player);
30
31
        $playerAction->introduce($this->location->getName());
32
        $playerAction->tell((string) $this->context);
33
34
        $visitors = $this->location->getVisitors();
35
        if (!$visitors->isEmpty()) {
36
            $playerAction->list(map(
37
                static fn(Visitor $visitor): array => [$visitor->getName() => (string) $visitor->getRole()],
38
                $visitors,
39
            ));
40
41
            $choice = $playerAction->askForChoice(
0 ignored issues
show
Unused Code introduced by
The assignment to $choice is dead and can be removed.
Loading history...
42
                'Where do you want to go?',
43
                array_merge($visitors->getItems(), ['Leave this place']),
44
            );
45
        }
46
47
        // travel
48
        return $playerAction->askForDecision('question', $this->decisionCollection);
49
    }
50
}
51