Completed
Push — master ( 566f81...189392 )
by Olivier
03:22
created

DBALLoadCommand   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 6

Test Coverage

Coverage 87.1%

Importance

Changes 0
Metric Value
wmc 10
lcom 2
cbo 6
dl 0
loc 63
ccs 27
cts 31
cp 0.871
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A configure() 0 11 1
B execute() 0 31 8
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') ? $input->getOption('groups') : $this->loader->getGroups();
52 3
        $groups = array_diff($groups, $input->getOption('exclude-groups', []));
0 ignored issues
show
Unused Code introduced by
The call to InputInterface::getOption() has too many arguments starting with array().

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
53
54 3
        if (0 === count($groups) && 0 < count($this->loader->getGroups())) {
55
            $io->warning('No group to load.');
56
57
            return;
58
        }
59
60 3
        if (!$input->getOption('fixtures-only')) {
61 2
            $statements = $this->loader->getSchema($groups)->toSql($this->connection->getDatabasePlatform());
62 2
            foreach ($statements as $statement) {
63
                $this->connection->query($statement);
64
            }
65
66 2
            $io->success('Schema created successfully.');
67
        }
68
69 3
        if (!$input->getOption('schema-only')) {
70 2
            foreach ($this->loader->getFixtures($groups) as $fixture) {
71
                $this->connection->insert($fixture[0], $fixture[1], $fixture[2]);
72
            }
73
74 2
            $io->success('Fixtures created successfully.');
75
        }
76 3
    }
77
}
78