Passed
Pull Request — master (#9)
by Derek Stephen
01:48
created

LoadFixturesCommand::execute()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 3
Bugs 0 Features 1
Metric Value
cc 4
eloc 18
c 3
b 0
f 1
nc 4
nop 2
dl 0
loc 29
ccs 0
cts 19
cp 0
crap 20
rs 9.6666
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Bone\BoneDoctrine\Command;
6
7
use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
8
use Doctrine\Common\DataFixtures\FixtureInterface;
9
use Doctrine\Common\DataFixtures\Loader;
10
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
11
use Doctrine\ORM\EntityManager;
12
use Symfony\Component\Console\Command\Command;
13
use Symfony\Component\Console\Input\InputInterface;
14
use Symfony\Component\Console\Input\InputOption;
15
use Symfony\Component\Console\Output\OutputInterface;
16
17
class LoadFixturesCommand extends Command
18
{
19
    private EntityManager $entityManager;
20
    private array $fixtures;
21
22 1
    public function __construct(EntityManager $entityManager, array $fixtures = [])
23
    {
24 1
        parent::__construct('migrant:fixtures');
25 1
        $this->entityManager = $entityManager;
26 1
        $this->fixtures = $fixtures;
27
    }
28
29 1
    protected function configure()
30
    {
31 1
        $this->setDescription('[fixtures] Loads data fixtures.');
32 1
        $this->setHelp('Loads data fixtures.');
33 1
        $this->addOption('purge', 'p', InputOption::VALUE_OPTIONAL, 'Purge the database before seeding', false);
34
    }
35
36
    /**
37
     * @param InputInterface $input
38
     * @param OutputInterface $output
39
     */
40
    protected function execute(InputInterface $input, OutputInterface $output)
41
    {
42
        $output->writeln('');
43
        $output->writeln('💀 Bone Framework database fixtures');
44
        $output->writeln('   Loading fixtures....');
45
        $output->writeln('');
46
        $loader = new Loader();
47
48
        if (\count($this->fixtures) === 0) {
49
            $output->writeln('🤷‍♂️ No fixtures found, exiting.');
50
            $output->writeln('');
51
52
            return Command::SUCCESS;
53
        }
54
55
        foreach ($this->fixtures as $fixture) {
56
            $instance = new $fixture();
57
58
            if ($instance instanceof FixtureInterface) {
59
                $output->writeln('   <info>Executing fixture ' . $fixture . '</info>');
60
                $loader->addFixture($instance);
61
            }
62
        }
63
        $output->writeln('');
64
65
        $executor = new ORMExecutor($this->entityManager, new ORMPurger());
66
        $executor->execute($loader->getFixtures(), $input->getOption('purge'));
67
68
        return Command::SUCCESS;
69
    }
70
}
71