Completed
Push — master ( 3db674...2e3aab )
by Remy
03:46
created

BuildIndexesCommand::execute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2.0145

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 18
ccs 11
cts 13
cp 0.8462
rs 9.4285
cc 2
eloc 10
nc 2
nop 2
crap 2.0145
1
<?php
2
3
namespace Pouzor\MongoDBBundle\Command;
4
5
use MongoDB\Client;
6
use MongoDB\Collection;
7
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
8
use Symfony\Component\Console\Helper\Table;
9
use Symfony\Component\Console\Input\InputInterface;
10
use Symfony\Component\Console\Input\InputOption;
11
use Symfony\Component\Console\Output\OutputInterface;
12
use Symfony\Component\Console\Style\SymfonyStyle;
13
use Symfony\Component\Yaml\Yaml;
14
15
class BuildIndexesCommand extends ContainerAwareCommand
16
{
17
18
    private $manager;
19
20
    /**
21
     * {@inheritdoc}
22
     */
23 1
    protected function configure()
24
    {
25 1
        $this
26 1
            ->setName('mongo:indexes:build')
27 1
            ->addOption('collection', 'c', InputOption::VALUE_OPTIONAL, 'Build indexes just for this collection')
28 1
            ->addOption('rebuild', 'r', InputOption::VALUE_NONE, 'Drop and create indexes')
29 1
            ->setDescription('Build indexes in a mongo database');
30 1
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 1
    protected function execute(InputInterface $input, OutputInterface $output)
36
    {
37 1
        $style = new SymfonyStyle($input, $output);
38
39 1
        $rebuild = $input->getOption('rebuild');
40 1
        $collection = $input->getOption('collection');
41
42 1
        $callback = function ($name) use ($style) {
43 1
            $style->comment(sprintf('%s : Ok', $name));
44 1
        };
45
46 1
        if ($collection) {
47
            $this->manager->getRepository($collection)->buildIndexes($callback);
48
        } else {
49 1
            $this->manager->buildIndexes($rebuild, $callback);
50 1
        }
51
52 1
    }
53
54 1
    public function setManager($manager) {
55
56 1
        $this->manager = $manager;
57
    }
58
}