Passed
Pull Request — master (#9)
by Derek Stephen
01:51
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\InputArgument;
14
use Symfony\Component\Console\Input\InputInterface;
15
use Symfony\Component\Console\Input\InputOption;
16
use Symfony\Component\Console\Output\OutputInterface;
17
18
class LoadFixturesCommand extends Command
19
{
20
    private EntityManager $entityManager;
21
    private array $fixtures;
22
23 1
    public function __construct(EntityManager $entityManager, array $fixtures = [])
24
    {
25 1
        parent::__construct('migrant:fixtures');
26 1
        $this->entityManager = $entityManager;
27 1
        $this->fixtures = $fixtures;
28
    }
29
30 1
    protected function configure()
31
    {
32 1
        $this->setDescription('[fixtures] Loads data fixtures.');
33 1
        $this->setHelp('Loads data fixtures.');
34 1
        $this->addArgument('purge', InputArgument::OPTIONAL, 'Purge the database before seeding', false);
35
    }
36
37
    /**
38
     * @param InputInterface $input
39
     * @param OutputInterface $output
40
     */
41
    protected function execute(InputInterface $input, OutputInterface $output)
42
    {
43
        $output->writeln('');
44
        $output->writeln('💀 Bone Framework database fixtures');
45
        $output->writeln('   Loading fixtures....');
46
        $output->writeln('');
47
        $loader = new Loader();
48
49
        if (\count($this->fixtures) === 0) {
50
            $output->writeln('🤷‍♂️ No fixtures found, exiting.');
51
            $output->writeln('');
52
53
            return Command::SUCCESS;
54
        }
55
56
        foreach ($this->fixtures as $fixture) {
57
            $instance = new $fixture();
58
59
            if ($instance instanceof FixtureInterface) {
60
                $output->writeln('   <info>Executing fixture ' . $fixture . '</info>');
61
                $loader->addFixture($instance);
62
            }
63
        }
64
        $output->writeln('');
65
66
        $executor = new ORMExecutor($this->entityManager, new ORMPurger());
67
        $executor->execute($loader->getFixtures(), !$input->getArgument('purge'));
68
69
        return Command::SUCCESS;
70
    }
71
}
72