LinkConfig   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 48
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 8 1
A initialize() 0 8 2
B execute() 0 27 3
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