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

PlayerIO   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 88
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 14
eloc 29
c 2
b 0
f 0
dl 0
loc 88
ccs 0
cts 51
cp 0
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A tell() 0 4 1
A askForDecision() 0 3 1
A askForChoice() 0 3 1
A note() 0 3 1
A section() 0 4 1
A listInventory() 0 12 1
A savePlayerState() 0 3 1
A clearOutput() 0 3 1
A introduce() 0 6 1
A list() 0 4 1
A askForInformation() 0 3 1
A askForConfirmation() 0 3 1
A __construct() 0 4 1
A askForDialogChoice() 0 4 1
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