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.

Extract   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 78
dl 0
loc 125
rs 10
c 1
b 0
f 1
ccs 0
cts 85
cp 0
wmc 15

2 Methods

Rating   Name   Duplication   Size   Complexity  
C execute() 0 50 14
A configure() 0 46 1
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->setDescription("Extract schema definitions from a mysqldump file");
59
60
        $this->addArgument(
61
            self::ARGUMENT_MYSQL_DUMP_FILE,
62
            InputArgument::OPTIONAL,
63
            '',
64
            'php://stdin'
65
        );
66
67
        $this->addOption(
68
            self::OPTION_SCHEMA_PATH,
69
            null,
70
            InputOption::VALUE_REQUIRED,
71
            '',
72
            './schema'
73
        );
74
75
        $this->addOption(
76
            self::OPTION_DATABASE,
77
            null,
78
            InputOption::VALUE_REQUIRED
79
        );
80
81
        $this->addOption(self::OPTION_WRITE);
82
        $this->addOption(self::OPTION_NO_WRITE);
83
    }
84
85
    /**
86
     * @param InputInterface $input
87
     * @param OutputInterface $output
88
     * @throws Exception
89
     */
90
    protected function execute(InputInterface $input, OutputInterface $output)
91
    {
92
        $this->mysqldump = $input->getArgument(self::ARGUMENT_MYSQL_DUMP_FILE);
93
94
        $this->schemaPath = $input->getOption(self::OPTION_SCHEMA_PATH);
95
        $this->databaseName = $input->getOption(self::OPTION_DATABASE);
96
97
        if ($input->getOption(self::OPTION_WRITE)) {
98
            $this->write = true;
99
        }
100
101
        $stream = TokenStream::newFromFile($this->mysqldump);
102
103
        $dump = new MysqlDump();
104
        try {
105
            $dump->parse($stream);
106
        } catch (RuntimeException $e) {
107
            throw new RuntimeException($stream->contextualise($e->getMessage()));
108
        } catch (Exception $e) {
109
            throw new Exception($e->getMessage() . "\n\n" . $e->getTraceAsString());
110
        }
111
112
        if ($this->write) {
113
            foreach ($dump->databases as $database) {
114
                $databaseName = ($database->name == '') ? $this->databaseName : $database->name;
115
                if ($databaseName == '') {
116
                    throw new RuntimeException("No database name specified in dump - please use --database=NAME to supply one");
117
                }
118
                $output = "{$this->schemaPath}/$databaseName";
119
120
                if (!is_dir($output)) {
121
                    if (!@mkdir($output, 0755, true)) {
122
                        throw new RuntimeException("Could not make directory $output");
123
                    }
124
                }
125
                foreach ($database->tables as $table) {
126
                    $path = "$output/{$table->getName()}.sql";
127
                    $text = '';
128
                    foreach ($table->getDDL() as $query) {
129
                        $text .= "$query;\n\n";
130
                    }
131
                    if (false === @file_put_contents($path, $text)) {
132
                        throw new RuntimeException("Could not write $path");
133
                    }
134
                    fprintf(STDERR, "wrote $path\n");
135
                }
136
            }
137
        } else {
138
            foreach ($dump->getDDL() as $query) {
139
                $output->writeln("$query;\n");
140
            }
141
        }
142
    }
143
}
144