Passed
Push — master ( d2e487...b38026 )
by Tobias
05:31
created

DeleteObsoleteCommand::execute()   B

Complexity

Conditions 11
Paths 84

Size

Total Lines 56
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 132

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 11
eloc 34
c 2
b 0
f 0
nc 84
nop 2
dl 0
loc 56
ccs 0
cts 46
cp 0
crap 132
rs 7.3166

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
/*
4
 * This file is part of the PHP Translation package.
5
 *
6
 * (c) PHP Translation team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Translation\Bundle\Command;
13
14
use Symfony\Component\Console\Command\Command;
15
use Symfony\Component\Console\Helper\ProgressBar;
16
use Symfony\Component\Console\Input\InputArgument;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Input\InputOption;
19
use Symfony\Component\Console\Output\OutputInterface;
20
use Symfony\Component\Console\Question\ConfirmationQuestion;
21
use Translation\Bundle\Catalogue\CatalogueFetcher;
22
use Translation\Bundle\Catalogue\CatalogueManager;
23
use Translation\Bundle\Service\ConfigurationManager;
24
use Translation\Bundle\Service\StorageManager;
25
26
/**
27
 * @author Tobias Nyholm <[email protected]>
28
 */
29
class DeleteObsoleteCommand extends Command
30
{
31
    use BundleTrait;
32
    use StorageTrait;
33
34
    protected static $defaultName = 'translation:delete-obsolete';
35
36
    /**
37
     * @var ConfigurationManager
38
     */
39
    private $configurationManager;
40
41
    /**
42
     * @var CatalogueManager
43
     */
44
    private $catalogueManager;
45
46
    /**
47
     * @var CatalogueFetcher
48
     */
49
    private $catalogueFetcher;
50
51
    public function __construct(
52
        StorageManager $storageManager,
53
        ConfigurationManager $configurationManager,
54
        CatalogueManager $catalogueManager,
55
        CatalogueFetcher $catalogueFetcher
56
    ) {
57
        $this->storageManager = $storageManager;
58
        $this->configurationManager = $configurationManager;
59
        $this->catalogueManager = $catalogueManager;
60
        $this->catalogueFetcher = $catalogueFetcher;
61
        parent::__construct();
62
    }
63
64
    protected function configure(): void
65
    {
66
        $this
67
            ->setName(self::$defaultName)
68
            ->setDescription('Delete all translations marked as obsolete.')
69
            ->addArgument('configuration', InputArgument::OPTIONAL, 'The configuration to use', 'default')
70
            ->addArgument('locale', InputArgument::OPTIONAL, 'The locale to use. If omitted, we use all configured locales.', null)
71
            ->addOption('bundle', 'b', InputOption::VALUE_REQUIRED, 'The bundle you want remove translations from.')
72
        ;
73
    }
74
75
    protected function execute(InputInterface $input, OutputInterface $output): int
76
    {
77
        $configName = $input->getArgument('configuration');
78
        $locales = [];
79
        if (null !== $inputLocale = $input->getArgument('locale')) {
80
            $locales = [$inputLocale];
81
        }
82
83
        $config = $this->configurationManager->getConfiguration($configName);
84
85
        $this->configureBundleDirs($input, $config);
86
        $this->catalogueManager->load($this->catalogueFetcher->getCatalogues($config, $locales));
87
88
        $storage = $this->getStorage($configName);
89
        $messages = $this->catalogueManager->findMessages(['locale' => $inputLocale, 'isObsolete' => true]);
90
91
        $messageCount = \count($messages);
92
        if (0 === $messageCount) {
93
            $output->writeln('No messages are obsolete');
94
95
            return 0;
96
        }
97
98
        if ($input->isInteractive()) {
99
            $helper = $this->getHelper('question');
100
            $question = new ConfirmationQuestion(\sprintf('You are about to remove %d translations. Do you wish to continue? (y/N) ', $messageCount), false);
101
            if (!$helper->ask($input, $output, $question)) {
102
                return 0;
103
            }
104
        }
105
106
        $progress = null;
107
        if (OutputInterface::VERBOSITY_NORMAL === $output->getVerbosity() && OutputInterface::VERBOSITY_QUIET !== $output->getVerbosity()) {
108
            $progress = new ProgressBar($output, $messageCount);
109
        }
110
        foreach ($messages as $message) {
111
            $storage->delete($message->getLocale(), $message->getDomain(), $message->getKey());
112
            if ($output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL) {
113
                $output->writeln(\sprintf(
114
                    'Deleted obsolete message "<info>%s</info>" from domain "<info>%s</info>" and locale "<info>%s</info>"',
115
                    $message->getKey(),
116
                    $message->getDomain(),
117
                    $message->getLocale()
118
                ));
119
            }
120
121
            if ($progress) {
122
                $progress->advance();
123
            }
124
        }
125
126
        if ($progress) {
127
            $progress->finish();
128
        }
129
130
        return 0;
131
    }
132
}
133