LoadGameCommand::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AardsGerds\Game\Infrastructure\Cli\Command;
6
7
use AardsGerds\Game\Event\Story\Story;
8
use AardsGerds\Game\Infrastructure\Cli\PlayerIO;
9
use AardsGerds\Game\Infrastructure\Persistence\PlayerState;
10
use AardsGerds\Game\Infrastructure\Persistence\PlayerStateException;
11
use Symfony\Component\Console\Command\Command;
12
use Symfony\Component\Console\Input\InputArgument;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Output\OutputInterface;
15
use Symfony\Component\Console\Style\SymfonyStyle;
16
17
final class LoadGameCommand extends Command
18
{
19
    protected static $defaultName = 'game:load';
20
21 11
    public function __construct(
22
        private PlayerState $playerState,
23
    ) {
24 11
        parent::__construct();
25 11
    }
26
27 11
    protected function configure(): void
28
    {
29
        $this
30 11
            ->setDescription('This command loads existing game')
31 11
            ->addArgument('player', InputArgument::REQUIRED, 'Player to load');
32 11
    }
33
34
    protected function execute(InputInterface $input, OutputInterface $output): int
35
    {
36
        $playerName = $input->getArgument('player');
37
        assert(is_string($playerName));
38
39
        $playerIO = new PlayerIO(new SymfonyStyle($input, $output), $this->playerState);
40
41
        try {
42
            $player = $this->playerState->load($playerName);
43
        } catch (PlayerStateException $exception) {
44
            $playerIO->note("Player {$playerName} doesn't exist.");
45
            return Command::FAILURE;
46
        }
47
48
        Story::continue($player->getCheckpoint(), $player, $playerIO);
49
50
        return Command::SUCCESS;
51
    }
52
}
53