Upload::execute()   B
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 27
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 14
nc 6
nop 2
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
use Symfony\Component\Finder\Finder;
9
10
class Upload extends Command
11
{
12
    protected function configure()
13
    {
14
        $this->setName('schema:upload')
15
             ->setDescription('Upload configuration set');
16
17
        $this->addArgument('name', IArg::REQUIRED, 'Config set name')
18
             ->addArgument('config-dir', IArg::REQUIRED, 'Config set folder. e.g /schema/conf');
19
    }
20
21
    protected function execute(InputInterface $input, OutputInterface $output)
22
    {
23
        $name = $input->getArgument('name');
24
        $configDir = $input->getArgument('config-dir');
25
26
        $path = '/configs';
27
28
        if (!$this->client->exists($path)) {
29
            $this->createFile($path);
30
        }
31
32
        $path = "{$path}/{$name}";
33
34
        $this->createFile($path);
35
36
        $finder = new Finder();
37
38
        foreach ($finder->in($configDir) as $file) {
39
            if ($file->isDir()) {
40
                $this->createFile("{$path}/{$file->getRelativePathname()}");
41
            }
42
43
            $this->createFile("{$path}/{$file->getRelativePathname()}", $file->getContents());
44
        }
45
46
        $output->writeln("<fg=green>The config set {$name} was uploaded</fg=green>");
47
    }
48
49
    /**
50
     * Create file or dir into ZooKeeper.
51
     *
52
     * @param string $filePath
53
     * @param string $value
54
     */
55
    private function createFile($filePath, $value = null)
56
    {
57
        if (!$this->client->exists($filePath)) {
58
            $this->client->create($filePath, $value, [
59
                [
60
                    'perms' => \Zookeeper::PERM_ALL,
61
                    'scheme' => 'world',
62
                    'id' => 'anyone',
63
                ],
64
            ]);
65
        }
66
67
        $this->client->set($filePath, $value);
68
    }
69
}
70