Completed
Push — master ( 0f65c5...868bcf )
by Alessandro
02:21
created

LoadFixturesCommand::loadFixture()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Facile\MongoDbBundle\Command;
6
7
use Facile\MongoDbBundle\Fixtures\MongoFixturesLoader;
8
use Facile\MongoDbBundle\Fixtures\MongoFixtureInterface;
9
use Symfony\Bundle\FrameworkBundle\Console\Application;
10
use Symfony\Component\Console\Input\InputArgument;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Input\InputOption;
13
use Symfony\Component\Console\Output\OutputInterface;
14
15
/**
16
 * Class LoadFixturesCommand.
17
 */
18
class LoadFixturesCommand extends AbstractCommand
19
{
20
    /**
21
     * {@inheritdoc}
22
     */
23 6
    protected function configure()
24
    {
25 6
        parent::configure();
26
        $this
27 6
            ->setName('mongodb:fixtures:load')
28 6
            ->addArgument('addFixturesPath', InputArgument::OPTIONAL, 'Add a path to search in for fixtures files')
29 6
            ->setDescription('Load fixtures and applies them');
30
        ;
31 6
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36 1
    protected function execute(InputInterface $input, OutputInterface $output)
37
    {
38 1
        $this->io->writeln('Loading mongo fixtures');
39
        /** @var Application $application */
40 1
        $application = $this->getApplication();
41
42 1
        $paths = [];
43
44 1
        if ($input->getArgument('addFixturesPath')) {
45 1
            $paths[] = $input->getArgument('addFixturesPath');
46
        }
47
48 1
        foreach ($application->getKernel()->getBundles() as $bundle) {
49 1
            $paths[] = $bundle->getPath().'/DataFixtures/Mongo';
50
        }
51
52 1
        $loader = new MongoFixturesLoader($this->getContainer());
53
54 1
        foreach ($paths as $path) {
55 1
            if (is_dir($path)) {
56 1
                $loader->loadFromDirectory($path);
57
            }
58 1
            if (is_file($path)) {
59 1
                $loader->loadFromFile($path);
60
            }
61
        }
62
63 1
        $fixtures = $loader->getLoadedClasses();
64 1
        if (empty($fixtures)) {
65
            throw new \InvalidArgumentException(
66
                sprintf('Could not find any class to load in: %s', "\n\n- ".implode("\n- ", $paths))
67
            );
68
        }
69
70 1
        foreach ($fixtures as $fixture){
71 1
            $this->loadFixture($fixture);
72
        }
73
74 1
        $this->io->writeln('Done.');
75 1
    }
76
77
    /**
78
     * @param MongoFixtureInterface $indexList
79
     */
80 1
    private function loadFixture(MongoFixtureInterface $indexList)
81
    {
82 1
        $indexList->loadData();
83 1
        $indexList->loadIndexes();
84 1
        $this->io->writeln('Loaded fixture: '. get_class($indexList));
85 1
    }
86
}
87