Completed
Push — master ( 189392...3a5307 )
by Olivier
03:25
created

DBALLoadCommand::execute()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 5.0592

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 13
cts 15
cp 0.8667
rs 9.2088
c 0
b 0
f 0
cc 5
nc 6
nop 2
crap 5.0592
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Shapin\Datagen\Bridge\Symfony\Bundle\Command;
6
7
use Shapin\Datagen\DBAL\Loader;
8
use Doctrine\DBAL\Connection;
9
use Symfony\Component\Console\Command\Command;
10
use Symfony\Component\Console\Input\InputInterface;
11
use Symfony\Component\Console\Input\InputOption;
12
use Symfony\Component\Console\Output\OutputInterface;
13
use Symfony\Component\Console\Style\SymfonyStyle;
14
15
class DBALLoadCommand extends Command
16
{
17
    private $connection;
18
    private $loader;
19
20 3
    public function __construct(Connection $connection, Loader $loader)
21
    {
22 3
        $this->connection = $connection;
23 3
        $this->loader = $loader;
24
25 3
        parent::__construct();
26 3
    }
27
28
    /**
29
     * {@inheritdoc}
30
     */
31 3
    protected function configure()
32
    {
33
        $this
34 3
            ->setName('shapin:datagen:dbal:load')
35 3
            ->setDescription('Load the DBAL schema.')
36 3
            ->addOption('groups', 'g', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Which groups should be loaded? (default: all)')
37 3
            ->addOption('exclude-groups', 'G', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Which groups should be excluded? (default: none)')
38 3
            ->addOption('schema-only', null, InputOption::VALUE_NONE, 'Load only schema.')
39 3
            ->addOption('fixtures-only', null, InputOption::VALUE_NONE, 'Load only fixtures.')
40
        ;
41 3
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 3
    protected function execute(InputInterface $input, OutputInterface $output)
47
    {
48 3
        $io = new SymfonyStyle($input, $output);
49 3
        $io->title('Create database schema with DBAL.');
50
51 3
        $groups = $input->getOption('groups');
52 3
        $excludeGroups = $input->getOption('exclude-groups');
53
54 3
        if (!$input->getOption('fixtures-only')) {
55 2
            $statements = $this->loader->getSchema($groups, $excludeGroups)->toSql($this->connection->getDatabasePlatform());
56 2
            foreach ($statements as $statement) {
57
                $this->connection->query($statement);
58
            }
59
60 2
            $io->success('Schema created successfully.');
61
        }
62
63 3
        if (!$input->getOption('schema-only')) {
64 2
            foreach ($this->loader->getFixtures($groups, $excludeGroups) as $fixture) {
65
                $this->connection->insert($fixture[0], $fixture[1], $fixture[2]);
66
            }
67
68 2
            $io->success('Fixtures created successfully.');
69
        }
70 3
    }
71
}
72