Passed
Push — master ( a665f8...0b64a5 )
by Paweł
03:11
created

Dialog::interlocutorDialog()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 9
ccs 0
cts 5
cp 0
rs 10
cc 2
nc 2
nop 1
crap 6
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 $interlocutor,
16
        private DialogOption $dialogOption,
17
        private PlayerAction $playerAction,
18
    ) {}
19
20
    public function __invoke(): void
21
    {
22
        $this->playerAction->tell("{$this->interlocutor->getName()}: {$this->dialogOption}");
23
24
        while (!$this->dialogOption->getResponses()->isEmpty()) {
25
            $this->playerDialog($this->dialogOption->getResponses());
26
        }
27
    }
28
29
    public function playerDialog(PlayerDialogOptionCollection $dialogOptions): void
30
    {
31
        $response = $this->playerAction->askForResponse($dialogOptions);
32
33
        $this->playerAction->tell("{$this->player->getName()}: {$response}");
34
        $this->interlocutorDialog($response->getResponse());
35
36
        if ($response instanceof QuitDialogOption) {
37
            $this->dialogOption->getResponses()->clear();
38
            return;
39
        }
40
41
        $this->dialogOption->getResponses()->remove($response);
42
43
        if ($response instanceof FightDialogOption) {
44
            $this->playerAction->askForConfirmation('Continue?');
45
            $response->getEvent()($this->player, $this->playerAction);
46
        }
47
    }
48
49
    public function interlocutorDialog(DialogOption $dialogOption): void
50
    {
51
        $this->playerAction->tell("{$this->interlocutor->getName()}: {$dialogOption}");
52
53
        if ($dialogOption->getResponses()->isEmpty()) {
54
            return;
55
        }
56
57
        $this->playerDialog($dialogOption->getResponses());
58
    }
59
}
60