LoadFixturesCommand::execute()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 29
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 4

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 19
cts 19
cp 1
crap 4
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 3
    public function __construct(EntityManager $entityManager, array $fixtures = [])
24
    {
25 3
        parent::__construct('migrant:fixtures');
26 3
        $this->entityManager = $entityManager;
27 3
        $this->fixtures = $fixtures;
28
    }
29
30 3
    protected function configure(): void
31
    {
32 3
        $this->setDescription('[fixtures] Loads data fixtures.');
33 3
        $this->setHelp('Loads data fixtures.');
34 3
        $this->addArgument('purge', InputArgument::OPTIONAL, 'Purge the database before seeding', false);
35
    }
36
37
    /**
38
     * @param InputInterface $input
39
     * @param OutputInterface $output
40
     */
41 2
    protected function execute(InputInterface $input, OutputInterface $output): int
42
    {
43 2
        $output->writeln('');
44 2
        $output->writeln('💀 Bone Framework database fixtures');
45 2
        $output->writeln('   Loading fixtures....');
46 2
        $output->writeln('');
47 2
        $loader = new Loader();
48
49 2
        if (\count($this->fixtures) === 0) {
50 1
            $output->writeln('🤷‍♂️ No fixtures found, exiting.');
51 1
            $output->writeln('');
52
53 1
            return Command::SUCCESS;
54
        }
55
56 1
        foreach ($this->fixtures as $fixture) {
57 1
            $instance = new $fixture();
58
59 1
            if ($instance instanceof FixtureInterface) {
60 1
                $output->writeln('   <info>Executing fixture ' . $fixture . '</info>');
61 1
                $loader->addFixture($instance);
62
            }
63
        }
64 1
        $output->writeln('');
65
66 1
        $executor = new ORMExecutor($this->entityManager, new ORMPurger());
67 1
        $executor->execute($loader->getFixtures(), !$input->getArgument('purge'));
68
69 1
        return Command::SUCCESS;
70
    }
71
}
72