Completed
Push — master ( 7c58aa...405235 )
by Tobias
09:45
created

DeleteObsoleteCommand::execute()   C

Complexity

Conditions 10
Paths 44

Size

Total Lines 54
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 110

Importance

Changes 0
Metric Value
dl 0
loc 54
ccs 0
cts 46
cp 0
rs 6.8372
c 0
b 0
f 0
cc 10
eloc 35
nc 44
nop 2
crap 110

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\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
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
22
/**
23
 * @author Tobias Nyholm <[email protected]>
24
 */
25
class DeleteObsoleteCommand extends ContainerAwareCommand
26
{
27
    use BundleTrait;
28
29 View Code Duplication
    protected function configure()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
30
    {
31
        $this
32
            ->setName('translation:delete-obsolete')
33
            ->setDescription('Delete all translations marked as obsolete.')
34
            ->addArgument('configuration', InputArgument::OPTIONAL, 'The configuration to use', 'default')
35
            ->addArgument('locale', InputArgument::OPTIONAL, 'The locale ot use. If omitted, we use all configured locales.', null)
36
            ->addOption('bundle', 'b', InputOption::VALUE_REQUIRED, 'The bundle you want remove translations from.')
37
        ;
38
    }
39
40
    protected function execute(InputInterface $input, OutputInterface $output)
41
    {
42
        $container = $this->getContainer();
43
        $configName = $input->getArgument('configuration');
44
        $locales = [];
45
        if (null !== $inputLocale = $input->getArgument('locale')) {
46
            $locales = [$inputLocale];
47
        }
48
49
        $catalogueManager = $container->get('php_translation.catalogue_manager');
50
        $config = $container->get('php_translation.configuration_manager')->getConfiguration($configName);
51
        $this->configureBundleDirs($input, $config);
52
        $catalogueManager->load($container->get('php_translation.catalogue_fetcher')->getCatalogues($config, $locales));
53
54
        $storage = $container->get('php_translation.storage.'.$configName);
55
        $messages = $catalogueManager->findMessages(['locale' => $inputLocale, 'isObsolete' => true]);
56
57
        $messageCount = count($messages);
58
        if (0 === $messageCount) {
59
            $output->writeln('No messages are obsolete');
60
61
            return;
62
        }
63
64
        $helper = $this->getHelper('question');
65
        $question = new ConfirmationQuestion(sprintf('You are about to remove %d translations. Do you wish to continue? (y/N) ', $messageCount), false);
66
        if (!$helper->ask($input, $output, $question)) {
67
            return;
68
        }
69
70
        $progress = null;
71
        if (OutputInterface::VERBOSITY_NORMAL === $output->getVerbosity() && OutputInterface::VERBOSITY_QUIET !== $output->getVerbosity()) {
72
            $progress = new ProgressBar($output, $messageCount);
73
        }
74
        foreach ($messages as $message) {
75
            $storage->delete($message->getLocale(), $message->getDomain(), $message->getKey());
76
            if ($output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL) {
77
                $output->writeln(sprintf(
78
                    'Deleted obsolete message "<info>%s</info>" from domain "<info>%s</info>" and locale "<info>%s</info>"',
79
                    $message->getKey(),
80
                    $message->getDomain(),
81
                    $message->getLocale()
82
                ));
83
            }
84
85
            if ($progress) {
86
                $progress->advance();
87
            }
88
        }
89
90
        if ($progress) {
91
            $progress->finish();
92
        }
93
    }
94
}
95