Completed
Push — master ( 6a0e64...33e708 )
by Tobias
03:13
created

DeleteEmptyCommand   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 103
Duplicated Lines 100 %

Coupling/Cohesion

Components 2
Dependencies 12

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 13
lcom 2
cbo 12
dl 103
loc 103
ccs 0
cts 69
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 12 12 1
A configure() 10 10 1
C execute() 56 56 11

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 View Code Duplication
class DeleteEmptyCommand extends Command
0 ignored issues
show
Duplication introduced by
This class 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
    use BundleTrait;
32
    use StorageTrait;
33
34
    protected static $defaultName = 'translation:delete-empty';
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 currently empty.')
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
        $this->configureBundleDirs($input, $config);
85
        $this->catalogueManager->load($this->catalogueFetcher->getCatalogues($config, $locales));
86
87
        $storage = $this->getStorage($configName);
88
        $messages = $this->catalogueManager->findMessages(['locale' => $inputLocale, 'isEmpty' => true]);
89
90
        $messageCount = \count($messages);
91
        if (0 === $messageCount) {
92
            $output->writeln('No messages are empty');
93
94
            return 0;
95
        }
96
97
        if ($input->isInteractive()) {
98
            $helper = $this->getHelper('question');
99
            $question = new ConfirmationQuestion(\sprintf('You are about to remove %d translations. Do you wish to continue? (y/N) ', $messageCount), false);
100
            if (!$helper->ask($input, $output, $question)) {
101
                return 0;
102
            }
103
        }
104
105
        $progress = null;
106
        if (OutputInterface::VERBOSITY_NORMAL === $output->getVerbosity() && OutputInterface::VERBOSITY_QUIET !== $output->getVerbosity()) {
107
            $progress = new ProgressBar($output, $messageCount);
108
        }
109
        foreach ($messages as $message) {
110
            $storage->delete($message->getLocale(), $message->getDomain(), $message->getKey());
111
            if ($output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL) {
112
                $output->writeln(\sprintf(
113
                    'Deleted empty message "<info>%s</info>" from domain "<info>%s</info>" and locale "<info>%s</info>"',
114
                    $message->getKey(),
115
                    $message->getDomain(),
116
                    $message->getLocale()
117
                ));
118
            }
119
120
            if ($progress) {
121
                $progress->advance();
122
            }
123
        }
124
125
        if ($progress) {
126
            $progress->finish();
127
        }
128
129
        return 0;
130
    }
131
}
132