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

DeleteObsoleteCommand::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 10
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 10
loc 10
ccs 0
cts 10
cp 0
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
crap 2
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