Completed
Push — master ( 89b849...9da0e2 )
by GBProd
02:23
created

ListIndexCommand::extractIndices()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 1 Features 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 1
b 1
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
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 View Code Duplication
    protected function execute(InputInterface $input, OutputInterface $output)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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
        $indices = $this->extractIndices($response->getData()['message']);
41
42 2
        foreach ($indices as $index) {
43 2
            $output->writeln($index);
44 2
        }
45 2
    }
46
47 2
    private function extractIndices($data)
48
    {
49 2
        $lines = preg_split('#\R#', $data);
50
51 2
        return array_map('trim', $lines);
52
    }
53
}
54