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

Dialog   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A playerDialog() 0 17 3
A __construct() 0 6 1
A __invoke() 0 6 2
A interlocutorDialog() 0 9 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 $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