Completed
Push — master ( 356e08...d08624 )
by Tobias
09:26
created

DownloadCommand::execute()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 21
ccs 0
cts 18
cp 0
rs 9.3142
c 2
b 0
f 0
cc 3
eloc 14
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\Component\Console\Command\Command;
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\CacheClearer;
21
use Translation\Bundle\Service\ConfigurationManager;
22
use Translation\Bundle\Service\StorageManager;
23
use Translation\Bundle\Model\Configuration;
24
25
/**
26
 * @author Tobias Nyholm <[email protected]>
27
 */
28
class DownloadCommand extends Command
29
{
30
    use BundleTrait;
31
32
    protected static $defaultName = 'translation:download';
33
34
    /**
35
     * @var StorageManager
36
     */
37
    private $storageManager;
38
39
    /**
40
     * @var ConfigurationManager
41
     */
42
    private $configurationManager;
43
44
    /**
45
     * @var CacheClearer
46
     */
47
    private $cacheCleaner;
48
49
    /**
50
     * @param StorageManager       $storageManager
51
     * @param ConfigurationManager $configurationManager
52
     * @param CacheClearer         $cacheCleaner
53
     */
54
    public function __construct(
55
        StorageManager $storageManager,
56
        ConfigurationManager $configurationManager,
57
        CacheClearer $cacheCleaner
58
    ) {
59
        $this->storageManager = $storageManager;
60
        $this->configurationManager = $configurationManager;
61
        $this->cacheCleaner = $cacheCleaner;
62
        parent::__construct();
63
    }
64
65 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...
66
    {
67
        $this
68
            ->setName(self::$defaultName)
69
            ->setDescription('Replace local messages with messages from remote')
70
            ->addArgument('configuration', InputArgument::OPTIONAL, 'The configuration to use', 'default')
71
            ->addOption('cache', null, InputOption::VALUE_NONE, 'Clear the cache if the translations have changed')
72
            ->addOption('bundle', 'b', InputOption::VALUE_REQUIRED, 'The bundle you want update translations from.')
73
        ;
74
    }
75
76
    protected function execute(InputInterface $input, OutputInterface $output)
77
    {
78
        $configName = $input->getArgument('configuration');
79
        $storage = $this->storageManager->getStorage($configName);
80
        $configuration = $this->configurationManager->getConfiguration($configName);
81
82
        $this->configureBundleDirs($input, $configuration);
0 ignored issues
show
Bug introduced by
It seems like $configuration defined by $this->configurationMana...figuration($configName) on line 80 can be null; however, Translation\Bundle\Comma...::configureBundleDirs() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
83
84
        if ($input->getOption('cache')) {
85
            $translationsDirectory = $configuration->getOutputDir();
86
            $md5BeforeDownload = $this->hashDirectory($translationsDirectory);
87
            $storage->download();
88
            $md5AfterDownload = $this->hashDirectory($translationsDirectory);
89
90
            if ($md5BeforeDownload !== $md5AfterDownload) {
91
                $this->cacheCleaner->clearAndWarmUp();
92
            }
93
        } else {
94
            $storage->download();
95
        }
96
    }
97
98
    private function hashDirectory($directory)
99
    {
100
        if (!is_dir($directory)) {
101
            return false;
102
        }
103
104
        $finder = new Finder();
105
        $finder->files()->in($directory)->notName('/~$/')->sortByName();
106
107
        $hash = hash_init('md5');
108
        foreach ($finder as $file) {
109
            hash_update_file($hash, $file->getRealPath());
110
        }
111
112
        return hash_final($hash);
113
    }
114
}
115