LinkConfig::initialize()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
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\Input\InputOption as IOpt;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
class LinkConfig extends Command
11
{
12
    protected function configure()
13
    {
14
        $this->setName('schema:link')
15
             ->setDescription('Link a collection to a configuration set');
16
17
        $this->addArgument('name', IArg::REQUIRED, 'Collection name')
18
             ->addOption('config-name', null, IOpt::VALUE_OPTIONAL, 'Name of configurations to use for this collection');
19
    }
20
21
    protected function initialize(InputInterface $input, OutputInterface $output)
22
    {
23
        if (is_null($input->getOption('config-name'))) {
24
            $input->setOption('config-name', $input->getArgument('name'));
25
        }
26
27
        parent::initialize($input, $output);
28
    }
29
30
    protected function execute(InputInterface $input, OutputInterface $output)
31
    {
32
        $name = $input->getArgument('name');
33
        $configName = $input->getOption('config-name');
34
        $configPath = "/configs/{$configName}";
35
36
        if (!$this->client->exists($configPath)) {
37
            $output->writeln("<fg=red>Config set {$configName} not found</fg=red>");
38
39
            return 1;
40
        }
41
42
        $collectionPath = "/collections/{$name}";
43
44
        if (!$this->client->exists($collectionPath)) {
45
            $output->writeln("<fg=red>Collection {$name} not found</fg=red>");
46
47
            return 1;
48
        }
49
50
        $value = [
51
            'configName' => $configName,
52
        ];
53
54
        $this->client->set($collectionPath, json_encode($value));
55
        $output->writeln("<fg=green>The collection {$name} was linked</fg=green>");
56
    }
57
}
58