ListIndexCommand::execute()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 16
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 0
loc 16
ccs 10
cts 10
cp 1
rs 9.4285
cc 3
eloc 8
nc 3
nop 2
crap 3
1
<?php
2
3
namespace GBProd\ElasticaExtraBundle\Command;
4
5
use Symfony\Component\Console\Input\InputArgument;
6
use Symfony\Component\Console\Input\InputInterface;
7
use Symfony\Component\Console\Input\InputOption;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
/**
11
 * Command to list indices
12
 *
13
 * @author gbprod <[email protected]>
14
 */
15
class ListIndexCommand extends ElasticaAwareCommand
16
{
17
    /**
18
     * {@inheritdoc}
19
     */
20 2
    protected function configure()
21
    {
22 2
        $this
23 2
            ->setName('elasticsearch:index:list')
24 2
            ->setDescription('Index list')
25 2
            ->addOption('pattern', '', InputOption::VALUE_REQUIRED, 'Index name filter pattern', null)
26 2
            ->addOption('client', null, InputOption::VALUE_REQUIRED, 'Client to use (if not default)', null)
27
        ;
28 2
    }
29
30
    /**
31
     * {@inheritdoc}
32
     */
33 2
    protected function execute(InputInterface $input, OutputInterface $output)
34
    {
35 2
        $client = $this->getClient($input->getOption('client'));
36
37 2
        $uri = sprintf('_cat/indices/%s?h=i', $input->getOption('pattern'));
38 2
        $response = $client->request($uri);
39
40 2
        if (isset($response->getData()['message'])) {
41
            $indices = $this->extractIndices($response->getData()['message']);
42 2
43 2
            foreach ($indices as $index) {
44 2
                $output->writeln($index);
45 2
            }
46
        }
47 2
48
    }
49 2
50
    private function extractIndices($data)
51 2
    {
52
        $lines = preg_split('#\R#', $data);
53
54
        return array_map('trim', $lines);
55
    }
56
}
57