ChopCommand::configure()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 34
Code Lines 25

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 25
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 34
ccs 0
cts 30
cp 0
crap 2
rs 9.52
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 Graze\ParallelProcess\Pool;
17
use Graze\ParallelProcess\Table;
18
use Graze\Sprout\Chop\Chopper;
19
use Graze\Sprout\Chop\TableChopperFactory;
20
use Graze\Sprout\Config\Config;
21
use Graze\Sprout\Db\DbTablePopulator;
22
use Graze\Sprout\Parser\FileTablePopulator;
23
use Graze\Sprout\Parser\ParsedSchema;
24
use Graze\Sprout\Parser\SchemaParser;
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 ChopCommand extends Command
33
{
34
    const OPTION_GROUP  = 'group';
35
    const OPTION_CONFIG = 'config';
36
    const OPTION_ALL    = 'all';
37
38
    const ARGUMENT_SCHEMA_TABLES = 'schemaTables';
39
40
    protected function configure()
41
    {
42
        $this->setName('chop');
43
        $this->setAliases(['truncate']);
44
        $this->setDescription('Chop down (truncate) all the tables');
45
46
        $this->addOption(
47
            static::OPTION_CONFIG,
48
            'c',
49
            InputOption::VALUE_OPTIONAL,
50
            'The configuration file to use',
51
            Config::DEFAULT_CONFIG_PATH
52
        );
53
54
        $this->addOption(
55
            static::OPTION_GROUP,
56
            'g',
57
            InputOption::VALUE_OPTIONAL,
58
            'The group to truncate'
59
        );
60
61
        $this->addOption(
62
            static::OPTION_ALL,
63
            'a',
64
            InputOption::VALUE_NONE,
65
            'Truncate all the tables in the schemas, '
66
            . 'if you specify tables in the <schemaTables> only those will be used. '
67
            . 'If this is not supplied only the files with seed data will be truncated'
68
        );
69
70
        $this->addArgument(
71
            static::ARGUMENT_SCHEMA_TABLES,
72
            InputArgument::OPTIONAL | InputArgument::IS_ARRAY,
73
            'Collection of schema and tables to use, examples: schema1 schema2 | schema1:* schema2:table1,table2'
74
        );
75
    }
76
77
    /**
78
     * @param \Symfony\Component\Console\Input\InputInterface   $input
79
     * @param \Symfony\Component\Console\Output\OutputInterface $output
80
     *
81
     * @return int|null
82
     * @throws \Exception
83
     */
84
    protected function execute(InputInterface $input, OutputInterface $output)
85
    {
86
        $schemas = $input->getArgument(static::ARGUMENT_SCHEMA_TABLES);
87
        $config = (new Config())->parse($input->getOption('config'));
88
        $group = $input->getOption('group') ?: $config->get(Config::CONFIG_DEFAULT_GROUP);
89
90
        $tablePopulator = $input->getOption(static::OPTION_ALL)
91
            ? new DbTablePopulator()
92
            : new FileTablePopulator(new Local('/'));
93
        $schemaParser = new SchemaParser($tablePopulator, $config, $group);
94
        $parsedSchemas = $schemaParser->extractSchemas($schemas);
95
96
        $useGlobal = count($parsedSchemas) <= 10;
97
98
        $globalPool = new Pool();
99
        $globalPool->setMaxSimultaneous($config->get(Config::CONFIG_DEFAULT_SIMULTANEOUS_PROCESSES));
100
101
        foreach ($parsedSchemas as $schema) {
102
            if ($useGlobal) {
103
                $pool = $globalPool;
104
            } else {
105
                $pool = new Pool(
106
                    [],
107
                    $config->get(Config::CONFIG_DEFAULT_SIMULTANEOUS_PROCESSES),
108
                    false,
109
                    ['chop', 'schema' => $schema->getSchemaName()]
110
                );
111
                $globalPool->add($pool);
112
            }
113
114
            $chopper = new Chopper($schema->getSchemaConfig(), $output, new TableChopperFactory($pool));
115
            $chopper->chop($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