Passed
Push — master ( df7727...a665f8 )
by Paweł
03:01
created

PlayerIO   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 13
eloc 27
c 2
b 0
f 0
dl 0
loc 82
ccs 0
cts 48
cp 0
rs 10

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