RocketDatabaseInsertCommand::execute()   B
last analyzed

Complexity

Conditions 4
Paths 6

Size

Total Lines 40
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 20

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 40
ccs 0
cts 29
cp 0
rs 8.5806
cc 4
eloc 23
nc 6
nop 2
crap 20
1
<?php
2
3
/*
4
 * This file is part of the "RocketORM" package.
5
 *
6
 * https://github.com/RocketORM/ORM
7
 *
8
 * For the full license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Rocket\ORM\Command;
13
14
use Rocket\ORM\Console\Command\AbstractCommand;
15
use Rocket\ORM\Console\RocketApplication;
16
use Rocket\ORM\Generator\Database\Table\DatabaseTableGenerator;
17
use Rocket\ORM\Generator\Schema\Schema;
18
use Symfony\Component\Console\Input\ArrayInput;
19
use Symfony\Component\Console\Input\InputInterface;
20
use Symfony\Component\Console\Output\ConsoleOutput;
21
use Symfony\Component\Console\Output\OutputInterface;
22
23
/**
24
 * @author Sylvain Lorinet <[email protected]>
25
 */
26
class RocketDatabaseInsertCommand extends AbstractCommand
27
{
28
    /**
29
     *
30
     */
31
    protected function configure()
32
    {
33
        $this
34
            ->setName('database:insert')
35
            ->setDescription('Insert all tables in databases. Databases should be created first.')
36
        ;
37
    }
38
39
    /**
40
     * @param InputInterface $input
41
     * @param OutputInterface $output
42
     *
43
     * @return int
44
     *
45
     * @throws \Exception
46
     */
47
    protected function execute(InputInterface $input, OutputInterface $output)
48
    {
49
        $schemas = $this->getSchemas($this->getSchemaPath());
50
51
        // Generating SQL
52
        $app = new RocketApplication();
53
        $command = $this->getRocketBuildSqlCommand();
54
        $app->add($command);
55
        $params = [
56
            'command' => $command->getName(),
57
        ];
58
59
        if (OutputInterface::VERBOSITY_DEBUG > $output->getVerbosity()) {
60
            $params['-q'] = true;
61
        }
62
63
        $app->setAutoExit(false);
64
        $app->run(new ArrayInput($params), new ConsoleOutput());
65
        $this->debug($output, '');
66
67
        $tableGenerator = new DatabaseTableGenerator($this->getSqlOutputPath());
68
69
        /** @var Schema $schema */
70
        foreach ($schemas as $schema) {
71
            $this->verbose($output, 'Inserting tables for database "' . $schema->database . '"... ', false);
72
            try {
73
                $tableGenerator->generate($schema);
74
            } catch (\Exception $e) {
75
                $this->verbose($output, '<error>FAIL</error>');
76
77
                throw $e;
78
            }
79
80
            $this->verbose($output, '<info>OK</info>');
81
        }
82
83
        $output->writeln('<info>All tables has been inserted.</info>');
84
85
        return 0;
86
    }
87
88
    /**
89
     * @return AbstractCommand
90
     */
91
    protected function getRocketBuildSqlCommand()
92
    {
93
        return new RocketGenerateSqlCommand();
94
    }
95
}
96