Passed
Push — master ( 0577a9...ac39e5 )
by Paweł
03:28
created

Dialog   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 8
eloc 18
c 2
b 0
f 0
dl 0
loc 46
ccs 0
cts 24
cp 0
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A npcDialog() 0 9 2
A playerDialog() 0 17 3
A __construct() 0 5 1
A __invoke() 0 6 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AardsGerds\Game\Dialog;
6
7
use AardsGerds\Game\Entity\Entity;
8
use AardsGerds\Game\Player\Player;
9
use AardsGerds\Game\Player\PlayerAction;
10
11
final class Dialog
12
{
13
    public function __construct(
14
        private Player $player,
15
        private Entity $npc,
16
        private NpcDialogOption $dialogOption,
17
    ) {}
18
19
    public function __invoke(PlayerAction $playerAction): void
20
    {
21
        $playerAction->tell("{$this->npc->getName()}: {$this->dialogOption}");
22
23
        while (!$this->dialogOption->getResponses()->isEmpty()) {
24
            $this->playerDialog($playerAction, $this->dialogOption->getResponses());
25
        }
26
    }
27
28
    private function playerDialog(PlayerAction $playerAction, PlayerDialogOptionCollection $dialogOptions): void
29
    {
30
        $playerDialogChoice = $playerAction->askForDialogChoice($dialogOptions);
31
32
        $playerAction->tell("{$this->player->getName()}: {$playerDialogChoice}");
33
        $this->npcDialog($playerAction, $playerDialogChoice->getResponse());
34
35
        if ($playerDialogChoice instanceof QuitDialogOption) {
36
            $this->dialogOption->getResponses()->clear();
37
            return;
38
        }
39
40
        $this->dialogOption->getResponses()->remove($playerDialogChoice);
41
42
        if ($playerDialogChoice instanceof FightDialogOption) {
43
            $playerAction->askForConfirmation('Continue?');
44
            $playerDialogChoice->getEvent()($this->player, $playerAction);
45
        }
46
    }
47
48
    private function npcDialog(PlayerAction $playerAction, NpcDialogOption $dialogOption): void
49
    {
50
        $playerAction->tell("{$this->npc->getName()}: {$dialogOption}");
51
52
        if ($dialogOption->getResponses()->isEmpty()) {
53
            return;
54
        }
55
56
        $this->playerDialog($playerAction, $dialogOption->getResponses());
57
    }
58
}
59