Code Duplication    Length = 103-104 lines in 2 locations

Command/DeleteEmptyCommand.php 1 location

@@ 29-131 (lines=103) @@
26
/**
27
 * @author Tobias Nyholm <[email protected]>
28
 */
29
class DeleteEmptyCommand extends Command
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

Command/DeleteObsoleteCommand.php 1 location

@@ 29-132 (lines=104) @@
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