DumpCommand::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 17
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 24
ccs 0
cts 21
cp 0
crap 2
rs 9.7
1
<?php
2
/**
3
 * This file is part of graze/sprout.
4
 *
5
 * Copyright © 2018 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\Config;
20
use Graze\Sprout\Dump\Dumper;
21
use Graze\Sprout\Dump\TableDumperFactory;
22
use Graze\Sprout\Parser\ParsedSchema;
23
use Graze\Sprout\Parser\SchemaParser;
24
use Graze\Sprout\Parser\FileTablePopulator;
25
use League\Flysystem\Adapter\Local;
26
use Symfony\Component\Console\Command\Command;
27
use Symfony\Component\Console\Input\InputArgument;
28
use Symfony\Component\Console\Input\InputInterface;
29
use Symfony\Component\Console\Input\InputOption;
30
use Symfony\Component\Console\Output\OutputInterface;
31
32
class DumpCommand extends Command
33
{
34
    const ARGUMENT_SCHEMA_TABLES = 'schemaTables';
35
36
    protected function configure()
37
    {
38
        $this->setName('dump');
39
        $this->setDescription('Dump the data for a given table to file.');
40
41
        $this->addOption(
42
            'config',
43
            'c',
44
            InputOption::VALUE_OPTIONAL,
45
            'The configuration file to use',
46
            Config::DEFAULT_CONFIG_PATH
47
        );
48
49
        $this->addOption(
50
            'group',
51
            'g',
52
            InputOption::VALUE_OPTIONAL,
53
            'The group to use'
54
        );
55
56
        $this->addArgument(
57
            static::ARGUMENT_SCHEMA_TABLES,
58
            InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
59
            'Collection of schema and tables to use, examples: schema1 schema2 | schema1:* schema2:table1,table2'
60
        );
61
    }
62
63
    /**
64
     * @param \Symfony\Component\Console\Input\InputInterface   $input
65
     * @param \Symfony\Component\Console\Output\OutputInterface $output
66
     *
67
     * @return int|null
68
     * @throws Exception
69
     */
70
    protected function execute(InputInterface $input, OutputInterface $output)
71
    {
72
        $schemas = $input->getArgument(static::ARGUMENT_SCHEMA_TABLES);
73
        $config = (new Config())->parse($input->getOption('config'));
74
        $group = $input->getOption('group') ?: $config->get(Config::CONFIG_DEFAULT_GROUP);
75
76
        $fileSystem = new Local('/');
77
        $tablePopulator = new FileTablePopulator($fileSystem);
78
        $schemaParser = new SchemaParser($tablePopulator, $config, $group);
79
        $parsedSchemas = $schemaParser->extractSchemas($schemas);
80
81
        $numTables = array_sum(array_map(
82
            function (ParsedSchema $schema) {
83
                return count($schema->getTables());
84
            },
85
            $parsedSchemas
86
        ));
87
88
        $useGlobal = $numTables <= 10;
89
90
        $globalPool = new Pool();
91
        $globalPool->setMaxSimultaneous($config->get(Config::CONFIG_DEFAULT_SIMULTANEOUS_PROCESSES));
92
93
        foreach ($parsedSchemas as $schema) {
94
            $output->writeln(sprintf(
95
                'Dumping <info>%d</info> tables in <info>%s</info> schema in group <info>%s</info> to <info>%s</info>',
96
                count($schema->getTables()),
97
                $schema->getSchemaName(),
98
                $group,
99
                $schema->getPath()
100
            ));
101
102
            if ($useGlobal) {
103
                $pool = $globalPool;
104
            } else {
105
                $pool = new Pool(
106
                    [],
107
                    $config->get(Config::CONFIG_DEFAULT_SIMULTANEOUS_PROCESSES),
108
                    false,
109
                    ['dump', 'schema' => $schema->getSchemaName()]
110
                );
111
                $globalPool->add($pool);
112
            }
113
114
            $dumper = new Dumper($schema->getSchemaConfig(), $output, new TableDumperFactory($pool), $fileSystem);
115
            $dumper->dump($schema->getPath(), $schema->getTables());
116
        }
117
118
        $processTable = new Table($output, $globalPool);
119
        $processTable->setShowSummary(true);
120
121
        if (!$processTable->run(0.1)) {
122
            return 1;
123
        }
124
125
        return 0;
126
    }
127
}
128