MoveAllCommand   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
eloc 45
c 3
b 1
f 0
dl 0
loc 85
rs 10
wmc 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 7 1
B doExecute() 0 71 9
1
<?php
2
3
namespace Startwind\Forrest\CliCommand\Repository\Command;
4
5
use Startwind\Forrest\CliCommand\Repository\RepositoryCommand;
6
use Startwind\Forrest\Repository\EditableRepository;
7
use Startwind\Forrest\Repository\ListAware;
8
use Startwind\Forrest\Repository\RepositoryCollection;
9
use Startwind\Forrest\Util\OutputHelper;
10
use Symfony\Component\Console\Command\Command;
11
use Symfony\Component\Console\Helper\ProgressBar;
12
use Symfony\Component\Console\Helper\QuestionHelper;
13
use Symfony\Component\Console\Input\InputArgument;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Input\InputOption;
16
use Symfony\Component\Console\Output\OutputInterface;
17
use Symfony\Component\Console\Question\ConfirmationQuestion;
18
19
class MoveAllCommand extends RepositoryCommand
20
{
21
    protected static $defaultName = 'repository:command:move:all';
22
    protected static $defaultDescription = 'Move all commands from one to another repository.';
23
24
    protected function configure()
25
    {
26
        parent::configure();
27
        $this->addArgument('sourceRepository', InputArgument::REQUIRED, 'The identifier of the repository you want to move the command.');
28
        $this->addArgument('destinationRepository', InputArgument::REQUIRED, 'The identifier of the repository you want to move the command.');
29
        $this->addOption('removeAfterMove', 'r', InputOption::VALUE_NONE, 'The identifier of the repository you want to move the command.');
30
        $this->addOption('prefix', 'p', InputOption::VALUE_OPTIONAL, 'The identifier of the repository you want to move the command.', '');
31
    }
32
33
    protected function doExecute(InputInterface $input, OutputInterface $output): int
34
    {
35
        $this->enrichRepositories();
36
37
        /** @var QuestionHelper $questionHelper */
38
        $questionHelper = $this->getHelper('question');
39
40
        $destinationRepositoryName = $input->getArgument('destinationRepository');
41
        $sourceRepositoryName = $input->getArgument('sourceRepository');
42
43
        $sourceRepository = $this->getRepositoryCollection()->getRepository($sourceRepositoryName);
44
        $destinationRepository = $this->getRepositoryCollection()->getRepository($destinationRepositoryName);
45
46
        if (!$sourceRepository instanceof ListAware) {
47
            throw new \RuntimeException('The given repository "' . $destinationRepositoryName . '" is not listable.');
48
        }
49
50
        if (!$destinationRepository instanceof EditableRepository) {
51
            throw new \RuntimeException('The given repository "' . $destinationRepositoryName . '" is read-only.');
52
        }
53
54
        $commands = $sourceRepository->getCommands();
55
56
        if (count($commands) == 0) {
57
            OutputHelper::writeInfoBox($output, 'No commands found in "' . $sourceRepositoryName . '". Cancelling move command.');
58
            return Command::SUCCESS;
59
        }
60
61
        OutputHelper::writeInfoBox($output, 'We are moving the following commands to the "' . $destinationRepositoryName . '" repository.');
62
63
        \Startwind\Forrest\Output\OutputHelper::renderCommands($output, $input, $questionHelper, $commands);
64
65
        $output->writeln('');
66
67
        $move = $questionHelper->ask($input, $output, new ConfirmationQuestion('  Are you sure you want to move these ' . count($commands) . ' command? (y/n) ', false));
68
69
        if (!$move) {
70
            return Command::FAILURE;
71
        }
72
73
        $prefix = $input->getOption('prefix');
74
75
        if ($prefix) {
76
            $prefix = $prefix . RepositoryCollection::COMMAND_SEPARATOR;
77
        }
78
79
        $progressBar = new ProgressBar($output, count($commands));
80
81
        $output->writeln('');
82
83
        foreach ($commands as $command) {
84
            $progressBar->advance();
85
86
            $command->setName($prefix . $command->getName());
87
            $progressBar->setMessage('Moving ' . $prefix . $command->getName());
88
89
            $destinationRepository->addCommand($command);
90
91
            if ($sourceRepository instanceof EditableRepository) {
92
                if ($input->getOption('removeAfterMove')) {
93
                    $sourceRepository->removeCommand($command->getName());
94
                }
95
            }
96
        }
97
98
        $progressBar->finish();
99
        $output->writeln('');
100
101
        OutputHelper::writeInfoBox($output, 'Successfully moved ' . count($commands) . ' commands to "' . $destinationRepositoryName . '".');
102
103
        return Command::SUCCESS;
104
    }
105
}
106