Completed
Push — master ( 96e687...3334c9 )
by Ilario
12:12 queued 02:13
created

LoadFixturesCommand::sortFixtures()   B

Complexity

Conditions 6
Paths 1

Size

Total Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 6

Importance

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