Completed
Push — master ( de177b...75719e )
by Olivier
11:13
created

DBALSchemaCreateCommand::execute()   B

Complexity

Conditions 6
Paths 9

Size

Total Lines 29

Duplication

Lines 10
Ratio 34.48 %

Code Coverage

Tests 13
CRAP Score 6.7717

Importance

Changes 0
Metric Value
dl 10
loc 29
ccs 13
cts 18
cp 0.7221
rs 8.8337
c 0
b 0
f 0
cc 6
nc 9
nop 2
crap 6.7717
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Shapin\Datagen\Bridge\Symfony\Bundle\Command;
6
7
use Shapin\Datagen\DBAL\Loader\SchemaLoader;
8
use Doctrine\DBAL\Connection;
9
use Doctrine\DBAL\Schema\Schema;
10
use Symfony\Component\Console\Command\Command;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Input\InputOption;
13
use Symfony\Component\Console\Output\OutputInterface;
14
use Symfony\Component\Console\Style\SymfonyStyle;
15
16
class DBALSchemaCreateCommand extends Command
17
{
18
    private $groups;
19
    private $connection;
20
21 1
    public function __construct(Connection $connection, array $groups)
22
    {
23 1
        $this->connection = $connection;
24 1
        $this->groups = $groups;
25
26 1
        parent::__construct();
27 1
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32 1
    protected function configure()
33
    {
34
        $this
35 1
            ->setName('datagen:dbal:schema:create')
36 1
            ->setDescription('Create the DBAL schema.')
37 1
            ->addOption('groups', 'g', InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Which groups should be loaded? (default: all)')
38
        ;
39 1
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 1
    protected function execute(InputInterface $input, OutputInterface $output)
45
    {
46 1
        $io = new SymfonyStyle($input, $output);
47 1
        $io->title('Create database schema with DBAL.');
48
49 1
        $groups = $this->groups;
50 1 View Code Duplication
        if ([] !== $wantedGroups = $input->getOption('groups')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
51
            $groups = [];
52
            foreach ($wantedGroups as $wantedGroup) {
0 ignored issues
show
Bug introduced by
The expression $wantedGroups of type string|array<integer,string>|boolean|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
53
                if (!array_key_exists($wantedGroup, $this->groups)) {
54
                    throw new \InvalidArgumentException(sprintf('Unknown group "%s". Available: ["%s"]', $wantedGroup, implode('", "', array_keys($this->groups))));
55
                }
56
57
                $groups[$wantedGroup] = $this->groups[$wantedGroup];
58
            }
59
        }
60
61 1
        $schemaLoader = new SchemaLoader(new Schema());
62 1
        foreach ($groups as $path) {
63 1
            $schemaLoader->load($path);
64
        }
65
66 1
        $statements = $schemaLoader->getSchema()->toSql($this->connection->getDatabasePlatform());
67 1
        foreach ($statements as $statement) {
68 1
            $this->connection->query($statement);
69
        }
70
71 1
        $io->success('Schema created successfully.');
72 1
    }
73
}
74