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

PlayerIO::askForDialogChoice()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AardsGerds\Game\Infrastructure\Cli;
6
7
use AardsGerds\Game\Dialog\PlayerDialogOption;
8
use AardsGerds\Game\Dialog\PlayerDialogOptionCollection;
9
use AardsGerds\Game\Event\Decision\Decision;
10
use AardsGerds\Game\Event\Decision\DecisionCollection;
11
use AardsGerds\Game\Infrastructure\Persistence\PlayerState;
12
use AardsGerds\Game\Inventory\Inventory;
13
use AardsGerds\Game\Inventory\InventoryItem;
14
use AardsGerds\Game\Player\Player;
15
use AardsGerds\Game\Player\PlayerAction;
16
use Symfony\Component\Console\Style\SymfonyStyle;
17
use function Lambdish\Phunctional\map;
18
19
final class PlayerIO implements PlayerAction
20
{
21
    public function __construct(
22
        private SymfonyStyle $io,
23
        private PlayerState $playerState,
24
    ) {}
25
26
    public function savePlayerState(Player $player): void
27
    {
28
        $this->playerState->save($player);
29
    }
30
31
    public function askForDecision(string $question, DecisionCollection $decisions): Decision
32
    {
33
        return $this->io->choice($question, $decisions->getItems());
34
    }
35
36
    public function askForDialogChoice(PlayerDialogOptionCollection $dialogOptions): PlayerDialogOption
37
    {
38
        sleep(1);
39
        return $this->io->choice('Your response', $dialogOptions->getItems());
40
    }
41
42
    public function askForChoice(string $question, array $choices): mixed
43
    {
44
        return $this->io->choice($question, $choices);
45
    }
46
47
    public function askForInformation(string $question): string
48
    {
49
        return $this->io->ask($question);
50
    }
51
52
    public function askForConfirmation(string $question): bool
53
    {
54
        return $this->io->confirm($question);
55
    }
56
57
    public function tell(string|array $message): void
58
    {
59
        sleep(1);
60
        $this->io->text($message);
61
    }
62
63
    public function list(array $items): void
64
    {
65
        sleep(1);
66
        $this->io->definitionList(...$items);
67
    }
68
69
    public function listInventory(Inventory $inventory): void
70
    {
71
        $this->io->table(
72
            ['Name', 'Description', 'Sell Value', 'Buy Value'],
73
            [...map(
74
                static fn(InventoryItem $inventoryItem): array => [
75
                    $inventoryItem->getName(),
76
                    $inventoryItem->getDescription(),
77
                    $inventoryItem->getSellValue(),
78
                    $inventoryItem->getBuyValue(),
79
                ],
80
                $inventory,
81
            )],
82
        );
83
    }
84
85
    public function introduce(string $message): void
86
    {
87
        sleep(1);
88
        $this->clearOutput();
89
        $this->io->title($message);
90
        sleep(1);
91
    }
92
93
    public function section(string $message): void
94
    {
95
        sleep(1);
96
        $this->io->section($message);
97
    }
98
99
    public function note(string $message): void
100
    {
101
        $this->io->note($message);
102
    }
103
104
    private function clearOutput(): void
105
    {
106
        $this->io->write("\033\143");
107
    }
108
}
109