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

BuildIndexesCommand   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 44
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 3

Test Coverage

Coverage 91.3%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
lcom 2
cbo 3
dl 0
loc 44
ccs 21
cts 23
cp 0.913
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 8 1
A execute() 0 18 2
A setManager() 0 4 1
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
}