Passed
Pull Request — master (#9)
by Derek Stephen
10:17
created

LoadFixturesCommand   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 32%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 25
c 1
b 0
f 1
dl 0
loc 50
ccs 8
cts 25
cp 0.32
rs 10
wmc 6

3 Methods

Rating   Name   Duplication   Size   Complexity  
A execute() 0 27 4
A configure() 0 5 1
A __construct() 0 5 1
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)) {
49
            $output->writeln('   No fixtures found, exiting.');
50
51
            return Command::SUCCESS;
52
        }
53
54
        foreach ($this->fixtures as $fixture) {
55
            $instance = new $fixture();
56
57
            if ($instance instanceof FixtureInterface) {
58
                $output->writeln('   Executing fixture ' . $fixture);
59
                $loader->addFixture($instance);
60
            }
61
        }
62
63
        $executor = new ORMExecutor($this->entityManager, new ORMPurger());
64
        $executor->execute($loader->getFixtures(), $input->getOption('purge'));
65
66
        return Command::SUCCESS;
67
    }
68
}
69