Completed
Pull Request — master (#53)
by
unknown
03:42
created

LoadFixturesCommand::prepareSearchPaths()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3

Importance

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