Passed
Pull Request — master (#1)
by Harry
02:11
created

SeedCommand   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 70
dl 0
loc 116
ccs 0
cts 85
cp 0
rs 10
c 0
b 0
f 0
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 31 1
B execute() 0 68 7
1
<?php
2
/**
3
 * This file is part of graze/sprout.
4
 *
5
 * Copyright (c) 2017 Nature Delivered Ltd. <https://www.graze.com>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license https://github.com/graze/sprout/blob/master/LICENSE.md
11
 * @link    https://github.com/graze/sprout
12
 */
13
14
namespace Graze\Sprout\Command;
15
16
use Exception;
17
use Graze\ParallelProcess\Pool;
18
use Graze\ParallelProcess\Table;
19
use Graze\Sprout\Config;
20
use Graze\Sprout\Parser\ParsedSchema;
21
use Graze\Sprout\Parser\SchemaParser;
22
use Graze\Sprout\Seed\Seeder;
23
use Graze\Sprout\Seed\TableSeederFactory;
24
use Symfony\Component\Console\Command\Command;
25
use Symfony\Component\Console\Input\ArrayInput;
26
use Symfony\Component\Console\Input\InputArgument;
27
use Symfony\Component\Console\Input\InputInterface;
28
use Symfony\Component\Console\Input\InputOption;
29
use Symfony\Component\Console\Output\OutputInterface;
30
31
class SeedCommand extends Command
32
{
33
    const OPTION_CONFIG          = 'config';
34
    const OPTION_CHOP            = 'chop';
35
    const OPTION_GROUP           = 'group';
36
    const ARGUMENT_SCHEMA_TABLES = 'schemaTables';
37
38
    protected function configure()
39
    {
40
        $this->setName('seed');
41
        $this->setDescription('Seed all the data for a given group, schema or table');
42
43
        $this->addOption(
44
            static::OPTION_CONFIG,
45
            'c',
46
            InputOption::VALUE_OPTIONAL,
47
            'The configuration file to use',
48
            Config::DEFAULT_CONFIG_PATH
49
        );
50
51
        $this->addOption(
52
            static::OPTION_CHOP,
53
            't',
54
            InputOption::VALUE_NONE,
55
            'chop (truncate) tables before seeding them'
56
        );
57
58
        $this->addOption(
59
            static::OPTION_GROUP,
60
            'g',
61
            InputOption::VALUE_OPTIONAL,
62
            'group to seed'
63
        );
64
65
        $this->addArgument(
66
            static::ARGUMENT_SCHEMA_TABLES,
67
            InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
68
            'Collection of schema and tables to use, examples: schema1 schema2 | schema1:* schema2:table1,table2'
69
        );
70
    }
71
72
    /**
73
     * @param \Symfony\Component\Console\Input\InputInterface   $input
74
     * @param \Symfony\Component\Console\Output\OutputInterface $output
75
     *
76
     * @return int|null
77
     * @throws Exception
78
     */
79
    protected function execute(InputInterface $input, OutputInterface $output)
80
    {
81
        $schemas = $input->getArgument(static::ARGUMENT_SCHEMA_TABLES);
82
        $config = (new Config())->parse($input->getOption('config'));
83
        $group = $input->getOption('group') ?: $config->get(Config::CONFIG_DEFAULT_GROUP);
84
85
        if ($input->getOption(static::OPTION_CHOP)) {
86
            $chopCommand = new ChopCommand();
87
            $exitCode = $chopCommand->run(
88
                new ArrayInput([
89
                    static::ARGUMENT_SCHEMA_TABLES => $schemas,
90
                    '--' . static::OPTION_CONFIG   => $input->getOption(static::OPTION_CONFIG),
91
                    '--' . static::OPTION_GROUP    => $group,
92
                ]),
93
                $output
94
            );
95
            if ($exitCode !== 0) {
96
                throw new \RuntimeException('failed to chop down the tables');
97
            }
98
        }
99
100
        $schemaParser = new SchemaParser($config, $group);
101
        $parsedSchemas = $schemaParser->extractSchemas($schemas);
102
103
        $numTables = array_sum(array_map(
104
            function (ParsedSchema $schema) {
105
                return count($schema->getTables());
106
            },
107
            $parsedSchemas
108
        ));
109
110
        $useGlobal = $numTables <= 10;
111
112
        $globalPool = new Pool();
113
        $globalPool->setMaxSimultaneous($config->get(Config::CONFIG_DEFAULT_SIMULTANEOUS_PROCESSES));
114
115
        foreach ($parsedSchemas as $schema) {
116
            $output->writeln(sprintf(
117
                'Seeding <info>%d</info> tables in <info>%s</info> schema in group <info>%s</info>',
118
                count($schema->getTables()),
119
                $schema->getSchameName(),
120
                $group
121
            ));
122
123
            if ($useGlobal) {
124
                $pool = $globalPool;
125
            } else {
126
                $pool = new Pool(
127
                    [],
128
                    $config->get(Config::CONFIG_DEFAULT_SIMULTANEOUS_PROCESSES),
129
                    false,
130
                    ['seed', 'schema' => $schema->getSchameName()]
131
                );
132
                $globalPool->add($pool);
133
            }
134
135
            $seeder = new Seeder($schema->getSchemaConfig(), $output, new TableSeederFactory($pool));
136
            $seeder->seed($schema->getPath(), $schema->getTables());
137
        }
138
139
        $processTable = new Table($output, $globalPool);
140
        $processTable->setShowSummary(true);
141
142
        if (!$processTable->run(0.1)) {
143
            return 1;
144
        }
145
146
        return 0;
147
    }
148
}
149