Download   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 53
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 53
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 8 1
B execute() 0 41 6
1
<?php
2
3
namespace Solr\Console\Command\Schema;
4
5
use Symfony\Component\Console\Input\InputArgument as IArg;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Output\OutputInterface;
8
9
class Download extends Command
10
{
11
    protected function configure()
12
    {
13
        $this->setName('schema:download')
14
             ->setDescription('Download configuration set');
15
16
        $this->addArgument('name', IArg::REQUIRED, 'Config set name')
17
             ->addArgument('dest', IArg::REQUIRED, 'Destination folder. Where the file is saved. e.g. /tmp');
18
    }
19
20
    protected function execute(InputInterface $input, OutputInterface $output)
21
    {
22
        $name = $input->getArgument('name');
23
        $dest = $input->getArgument('dest');
24
25
        $path = '/configs';
26
27
        if (!$this->client->exists($path)) {
28
            $output->writeln("<fg=red>Config set {$name} not found</fg=red>");
29
30
            return 1;
31
        }
32
33
        $path = "{$path}/{$name}";
34
35
        if (!$this->client->exists($path)) {
36
            $output->writeln("<fg=red>Config set {$name} not found</fg=red>");
37
38
            return 1;
39
        }
40
41
        $files = $this->client->getChildren($path);
42
43
        if (count($files) === 0) {
44
            $output->writeln("<fg=red>Files not found in config set {$name}</fg=red>");
45
46
            return 1;
47
        }
48
49
        $zip = new \ZipArchive();
50
        $zip->open("{$dest}/{$name}.zip", \ZipArchive::CREATE);
51
52
        foreach ($files as $file) {
53
            $content = $this->client->get("{$path}/{$file}");
54
            $zip->addFromString($file, $content);
55
        }
56
57
        if ($zip->close()) {
58
            $output->writeln("<fg=green>The config set {$name} was saved in {$dest}</fg=green>");
59
        }
60
    }
61
}
62