Remove   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

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

4 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 7 1
A execute() 0 22 3
A recursiveDelete() 0 10 2
B isLinked() 0 25 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 Remove extends Command
10
{
11
    protected function configure()
12
    {
13
        $this->setName('schema:delete')
14
             ->setDescription('Remove configuration set');
15
16
        $this->addArgument('name', IArg::REQUIRED, 'Config set name');
17
    }
18
19
    protected function execute(InputInterface $input, OutputInterface $output)
20
    {
21
        $name = $input->getArgument('name');
22
23
        $path = '/configs/'.$name;
24
25
        if (!$this->client->exists($path)) {
26
            $output->writeln("<fg=red>The config set {$name} not found</fg=red>");
27
28
            return 1;
29
        }
30
31
        if ($this->isLinked($name)) {
32
            $output->writeln('<fg=red>Config set was linked with other collections</fg=red>');
33
34
            return 1;
35
        }
36
37
        $this->recursiveDelete($path);
38
39
        $output->writeln("<fg=green>The config set {$name} was deleted</fg=green>");
40
    }
41
42
    private function recursiveDelete($path)
43
    {
44
        $children = $this->client->getChildren($path);
45
46
        foreach ($children as $child) {
47
            $this->recursiveDelete($path.'/'.$child);
48
        }
49
50
        $this->client->delete($path);
51
    }
52
53
    private function isLinked($name)
54
    {
55
        if (!is_string($name)) {
56
            throw new \InvalidArgumentException('Config set name must be string');
57
        }
58
59
        $path = '/collections';
60
61
        if (!$this->client->exists($path)) {
62
            return false;
63
        }
64
65
        $collections = $this->client->getChildren($path);
66
67
        foreach ($collections as $collection) {
68
            $config = $this->client->get($path.'/'.$collection);
69
            $config = json_decode($config, true);
70
71
            if (isset($config['configName']) && $config['configName'] === $name) {
72
                return true;
73
            }
74
        }
75
76
        return false;
77
    }
78
}
79