Completed
Push — master ( 6726c8...0b808f )
by Olivier
37:56 queued 36:26
created

DownloadCommand::execute()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 43

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
dl 0
loc 43
ccs 0
cts 32
cp 0
rs 8.9208
c 0
b 0
f 0
cc 5
nc 12
nop 2
crap 30
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\Input\InputArgument;
16
use Symfony\Component\Console\Input\InputInterface;
17
use Symfony\Component\Console\Input\InputOption;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Symfony\Component\Console\Style\SymfonyStyle;
20
use Symfony\Component\Finder\Finder;
21
use Translation\Bundle\Catalogue\CatalogueWriter;
22
use Translation\Bundle\Service\CacheClearer;
23
use Translation\Bundle\Service\ConfigurationManager;
24
use Translation\Bundle\Service\StorageManager;
25
26
/**
27
 * @author Tobias Nyholm <[email protected]>
28
 */
29
class DownloadCommand extends Command
30
{
31
    use BundleTrait;
32
    use StorageTrait;
33
34
    protected static $defaultName = 'translation:download';
35
36
    private $configurationManager;
37
    private $cacheCleaner;
38
    private $catalogueWriter;
39
40
    public function __construct(
41
        StorageManager $storageManager,
42
        ConfigurationManager $configurationManager,
43
        CacheClearer $cacheCleaner,
44
        CatalogueWriter $catalogueWriter
45
    ) {
46
        $this->storageManager = $storageManager;
47
        $this->configurationManager = $configurationManager;
48
        $this->cacheCleaner = $cacheCleaner;
49
        $this->catalogueWriter = $catalogueWriter;
50
51
        parent::__construct();
52
    }
53
54 View Code Duplication
    protected function configure(): void
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...
55
    {
56
        $this
57
            ->setName(self::$defaultName)
58
            ->setDescription('Replace local messages with messages from remote')
59
            ->setHelp(<<<EOT
60
The <info>%command.name%</info> will erase all your local translations and replace them with translations downloaded from the remote.
61
EOT
62
            )
63
            ->addArgument('configuration', InputArgument::OPTIONAL, 'The configuration to use', 'default')
64
            ->addOption('cache', null, InputOption::VALUE_NONE, '[DEPRECATED] Cache is now automatically cleared when translations have changed.')
65
            ->addOption('bundle', 'b', InputOption::VALUE_REQUIRED, 'The bundle you want update translations from.')
66
        ;
67
    }
68
69
    protected function execute(InputInterface $input, OutputInterface $output): int
70
    {
71
        $io = new SymfonyStyle($input, $output);
72
73
        if ($input->getOption('cache')) {
74
            $message = 'The --cache option is deprecated as it\'s now the default behaviour of this command.';
75
76
            $io->note($message);
77
            @\trigger_error($message, E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
78
        }
79
80
        $configName = $input->getArgument('configuration');
81
        $config = $this->configurationManager->getConfiguration($configName);
82
        $storage = $this->getStorage($configName);
83
84
        $this->configureBundleDirs($input, $config);
85
86
        $translationsDirectory = $config->getOutputDir();
87
        $md5BeforeDownload = $this->hashDirectory($translationsDirectory);
88
89
        $catalogues = $storage->download();
90
        $this->catalogueWriter->writeCatalogues($config, $catalogues);
91
92
        $translationsCount = 0;
93
        foreach ($catalogues as $locale => $catalogue) {
94
            foreach ($catalogue->all() as $domain => $messages) {
95
                $translationsCount += \count($messages);
96
            }
97
        }
98
99
        $io->text("<info>$translationsCount</info> translations have been downloaded.");
100
101
        $md5AfterDownload = $this->hashDirectory($translationsDirectory);
102
103
        if ($md5BeforeDownload !== $md5AfterDownload) {
104
            $io->success('Translations updated successfully!');
105
            $this->cacheCleaner->clearAndWarmUp();
106
        } else {
107
            $io->success('All translations were up to date.');
108
        }
109
110
        return 0;
111
    }
112
113
    /**
114
     * @return bool|string
115
     */
116
    private function hashDirectory(string $directory)
117
    {
118
        if (!\is_dir($directory)) {
119
            return false;
120
        }
121
122
        $finder = new Finder();
123
        $finder->files()->in($directory)->notName('/~$/')->sortByName();
124
125
        $hash = \hash_init('md5');
126
        foreach ($finder as $file) {
127
            \hash_update_file($hash, $file->getRealPath());
128
        }
129
130
        return \hash_final($hash);
131
    }
132
}
133