Completed
Push — master ( aae586...fa230b )
by Tobias
12:29
created

DownloadCommand::execute()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 24
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 24
ccs 0
cts 19
cp 0
rs 8.9713
cc 3
eloc 15
nc 3
nop 2
crap 12
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\Input\InputArgument;
16
use Symfony\Component\Console\Input\InputOption;
17
use Symfony\Component\Console\Input\InputInterface;
18
use Symfony\Component\Console\Output\OutputInterface;
19
use Symfony\Component\Finder\Finder;
20
use Translation\Bundle\Service\StorageService;
21
use Translation\Bundle\Model\Configuration;
22
23
/**
24
 * @author Tobias Nyholm <[email protected]>
25
 */
26
class DownloadCommand extends ContainerAwareCommand
27
{
28
    protected function configure()
29
    {
30
        $this
31
            ->setName('translation:download')
32
            ->setDescription('Replace local messages with messages from remote')
33
            ->addArgument('configuration', InputArgument::OPTIONAL, 'The configuration to use', 'default')
34
            ->addOption('cache', null, InputOption::VALUE_NONE, 'Clear the cache if the translations have changed')
35
        ;
36
    }
37
38
    protected function execute(InputInterface $input, OutputInterface $output)
39
    {
40
        $container = $this->getContainer();
41
        $configName = $input->getArgument('configuration');
42
43
        /** @var StorageService $storage */
44
        $storage = $container->get('php_translation.storage.'.$configName);
45
        /** @var Configuration $configuration */
46
        $configuration = $this->getContainer()->get('php_translation.configuration.'.$configName);
47
48
        if ($input->getOption('cache')) {
49
            $translationsDirectory = $configuration->getOutputDir();
50
            $md5BeforeDownload = $this->hashDirectory($translationsDirectory);
51
            $storage->download();
52
            $md5AfterDownload = $this->hashDirectory($translationsDirectory);
53
54
            if ($md5BeforeDownload !== $md5AfterDownload) {
55
                $cacheClearer = $this->getContainer()->get('php_translation.cache_clearer');
56
                $cacheClearer->clearAndWarmUp();
57
            }
58
        } else {
59
            $storage->download();
60
        }
61
    }
62
63
    private function hashDirectory($directory)
64
    {
65
        if (!is_dir($directory)) {
66
            return false;
67
        }
68
69
        $finder = new Finder();
70
        $finder->files()->in($directory)->notName('/~$/')->sortByName();
71
72
        $hash = hash_init('md5');
73
        foreach ($finder as $file) {
74
            hash_update_file($hash, $file->getRealPath());
75
        }
76
77
        return hash_final($hash);
78
    }
79
}
80