GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Passed
Push — master ( 42a542...6d81cd )
by Burhan
02:01
created

Extract::configure()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 44
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 33
nc 1
nop 0
dl 0
loc 44
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace Graze\Morphism\Command;
4
5
use Exception;
6
use Graze\Morphism\Parse\MysqlDump;
7
use Graze\Morphism\Parse\TokenStream;
8
use RuntimeException;
9
use Symfony\Component\Console\Command\Command;
10
use Symfony\Component\Console\Input\InputArgument;
11
use Symfony\Component\Console\Input\InputInterface;
12
use Symfony\Component\Console\Input\InputOption;
13
use Symfony\Component\Console\Output\OutputInterface;
14
15
class Extract extends Command
16
{
17
    const COMMAND_NAME              = 'extract';
18
19
    // Command line arguments
20
    const ARGUMENT_MYSQL_DUMP_FILE  = 'mysql-dump-file';
21
22
    // Command line options
23
    const OPTION_SCHEMA_PATH        = 'schema-path';
24
    const OPTION_DATABASE           = 'database';
25
    const OPTION_WRITE              = 'write';
26
    const OPTION_NO_WRITE           = 'no-write';
27
28
    /** @var string */
29
    private $schemaPath = './schema';
30
    /** @var bool */
31
    private $write = false;
32
    /** @var string|null */
33
    private $mysqldump = null;
34
    /** @var string|null */
35
    private $databaseName = null;
36
37
    protected function configure()
38
    {
39
        $this->setName(self::COMMAND_NAME);
40
41
        $helpText = sprintf(
42
            "Usage: %s [OPTIONS] [MYSQL-DUMP-FILE]\n" .
43
            "Extracts schema definition(s) from a mysqldump file. Multiple databases may\n" .
44
            "be defined in the dump, and they will be extracted to separate directories.\n" .
45
            "You might use this tool when initialising the schema directory from a dump\n" .
46
            "created on a production server with 'mysqldump --no-data'.\n" .
47
            "\n" .
48
            "OPTIONS\n" .
49
            "  -h, -help, --help   display this message, and exit\n" .
50
            "  --schema-path=PATH  location of schemas; default: ./schema\n" .
51
            "  --database=NAME     name of database if not specified in dump\n" .
52
            "  --[no-]write        write schema files to schema path; default: no\n" .
53
            "",
54
            self::COMMAND_NAME
55
        );
56
        $this->setHelp($helpText);
57
58
        $this->addArgument(
59
            self::ARGUMENT_MYSQL_DUMP_FILE,
60
            InputArgument::OPTIONAL,
61
            null,
62
            'php://stdin'
63
        );
64
65
        $this->addOption(
66
            self::OPTION_SCHEMA_PATH,
67
            null,
68
            InputOption::VALUE_REQUIRED,
69
            null,
70
            './schema'
71
        );
72
73
        $this->addOption(
74
            self::OPTION_DATABASE,
75
            null,
76
            InputOption::VALUE_REQUIRED
77
        );
78
79
        $this->addOption(self::OPTION_WRITE);
80
        $this->addOption(self::OPTION_NO_WRITE);
81
    }
82
83
    /**
84
     * @param InputInterface $input
85
     * @param OutputInterface $output
86
     * @throws Exception
87
     */
88
    protected function execute(InputInterface $input, OutputInterface $output)
89
    {
90
        $this->mysqldump = $input->getArgument(self::ARGUMENT_MYSQL_DUMP_FILE);
91
92
        $this->schemaPath = $input->getOption(self::OPTION_SCHEMA_PATH);
93
        $this->databaseName = $input->getOption(self::OPTION_DATABASE);
94
95
        if ($input->getOption(self::OPTION_WRITE)) {
96
            $this->write = true;
97
        }
98
99
        $stream = TokenStream::newFromFile($this->mysqldump);
100
101
        $dump = new MysqlDump();
102
        try {
103
            $dump->parse($stream);
104
        } catch (RuntimeException $e) {
105
            throw new RuntimeException($stream->contextualise($e->getMessage()));
106
        } catch (Exception $e) {
107
            throw new Exception($e->getMessage() . "\n\n" . $e->getTraceAsString());
108
        }
109
110
        if ($this->write) {
111
            foreach ($dump->databases as $database) {
112
                $databaseName = ($database->name == '') ? $this->databaseName : $database->name;
113
                if ($databaseName == '') {
114
                    throw new RuntimeException("No database name specified in dump - please use --database=NAME to supply one");
115
                }
116
                $output = "{$this->schemaPath}/$databaseName";
117
118
                if (!is_dir($output)) {
119
                    if (!@mkdir($output, 0755, true)) {
120
                        throw new RuntimeException("Could not make directory $output");
121
                    }
122
                }
123
                foreach ($database->tables as $table) {
124
                    $path = "$output/{$table->name}.sql";
125
                    $text = '';
126
                    foreach ($table->getDDL() as $query) {
127
                        $text .= "$query;\n\n";
128
                    }
129
                    if (false === @file_put_contents($path, $text)) {
130
                        throw new RuntimeException("Could not write $path");
131
                    }
132
                    fprintf(STDERR, "wrote $path\n");
133
                }
134
            }
135
        } else {
136
            foreach ($dump->getDDL() as $query) {
137
                echo "$query;\n\n";
138
            }
139
        }
140
    }
141
}
142