Remove   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 46
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 7 1
B execute() 0 27 5
1
<?php
2
3
namespace Solr\Console\Command\Collection;
4
5
use GuzzleHttp\Exception\ClientException;
6
use GuzzleHttp\Exception\ConnectException;
7
use Symfony\Component\Console\Input\InputArgument as IArg;
8
use Symfony\Component\Console\Input\InputInterface;
9
use Symfony\Component\Console\Output\OutputInterface;
10
11
/**
12
 * Remove collection.
13
 */
14
class Remove extends Command
15
{
16
    protected function configure()
17
    {
18
        $this->setName('collection:delete')
19
             ->setDescription('Delete a collection');
20
21
        $this->addArgument('name', IArg::REQUIRED, 'Collection name');
22
    }
23
24
    /**
25
     * Execute remove command. Remove a collection from the Solr.
26
     *
27
     * @param Symfony\Component\Console\Input\InputInterface  $input
28
     * @param Symfony\Component\Console\Output\OutpuInterface $output
29
     *
30
     * @return int
31
     */
32
    protected function execute(InputInterface $input, OutputInterface $output)
33
    {
34
        try {
35
            $name = $input->getArgument('name');
36
            $result = $this->client
37
                           ->get("admin/collections?action=DELETE&name={$name}&wt=json")
38
                           ->json();
39
40
            if (isset($result['success'])) {
41
                $output->writeln('<fg=green>The collection was deleted</fg=green>');
42
            }
43
44
            return 0;
45
        } catch (ClientException $e) {
46
            $response = $e->getResponse()->json();
47
48
            if (isset($response['error']['msg'])) {
49
                $output->writeln("<fg=red>{$response['error']['msg']}</fg=red>");
50
            }
51
52
            return 1;
53
        } catch (ConnectException $e) {
54
            $output->writeln('<fg=red>The connection failed for host</fg=red>');
55
56
            return 1;
57
        }
58
    }
59
}
60