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

VisitEvent::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 23
ccs 0
cts 14
cp 0
rs 9.8333
cc 2
nc 2
nop 2
crap 6
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