LoadGameCommand   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 41.18%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 16
c 1
b 0
f 0
dl 0
loc 34
ccs 7
cts 17
cp 0.4118
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 5 1
A __construct() 0 4 1
A execute() 0 17 2
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