Passed
Push — master ( 6a7e8f...cdfe84 )
by Paweł
03:06
created

LoadGameCommand::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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