Completed
Push — master ( 286948...1381e7 )
by Freek
01:42
created

MySql::getContentsOfCredentialsFile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 12
rs 9.4285
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
3
namespace Spatie\DbDumper\Databases;
4
5
use Spatie\DbDumper\DbDumper;
6
use Spatie\DbDumper\Exceptions\CannotStartDump;
7
use Symfony\Component\Process\Process;
8
9
class MySql extends DbDumper
10
{
11
    protected $dbName;
12
    protected $userName;
13
    protected $password;
14
    protected $host = 'localhost';
15
    protected $port = 3306;
16
    protected $socket;
17
    protected $dumpBinaryPath = '';
18
    protected $useExtendedInserts = true;
19
20
    public function getDbName() : string
21
    {
22
        return $this->dbName;
23
    }
24
25
    public function setDbName(string $dbName) : MySql
26
    {
27
        $this->dbName = $dbName;
28
29
        return $this;
30
    }
31
32
    public function setUserName(string $userName) : MySql
33
    {
34
        $this->userName = $userName;
35
36
        return $this;
37
    }
38
39
    public function setPassword(string $password) : MySql
40
    {
41
        $this->password = $password;
42
43
        return $this;
44
    }
45
46
    public function setHost(string $host) : MySql
47
    {
48
        $this->host = $host;
49
50
        return $this;
51
    }
52
53
    public function setPort(int $port) : MySql
54
    {
55
        $this->port = $port;
56
57
        return $this;
58
    }
59
60
    public function setSocket(int $socket) : MySql
61
    {
62
        $this->socket = $socket;
63
64
        return $this;
65
    }
66
67
    public function setDumpBinaryPath(string $dumpBinaryPath) : MySql
68
    {
69
        if ($dumpBinaryPath !== '' && substr($dumpBinaryPath, -1) !== '/') {
70
            $dumpBinaryPath .= '/';
71
        }
72
73
        $this->dumpBinaryPath = $dumpBinaryPath;
74
75
        return $this;
76
    }
77
78
    public function useExtendedInserts() : MySql
79
    {
80
        $this->useExtendedInserts = true;
81
82
        return $this;
83
    }
84
85
    public function dontUseExtendedInserts() : MySql
86
    {
87
        $this->useExtendedInserts = false;
88
89
        return $this;
90
    }
91
92
    /*
93
     * Dump the contents of the database to the given file.
94
     */
95
    public function dumpToFile(string $dumpFile)
96
    {
97
        $this->guardAgainstIncompleteCredentials();
98
99
        $tempFileHandle = tmpfile();
100
101
        fwrite($tempFileHandle, $this->getContentsOfCredentialsFile());
102
103
        $temporaryCredentialsFile = stream_get_meta_data($tempFileHandle)['uri'];
104
105
        $command = $this->getDumpCommand($dumpFile, $temporaryCredentialsFile);
106
107
        $process = new Process($command);
108
109
        $process->run();
110
111
        $this->checkIfDumpWasSuccessFul($process, $dumpFile);
112
    }
113
114
    /**
115
     * Get the command that should be performed to dump the database.
116
     */
117
    public function getDumpCommand(string $dumpFile, string $temporaryCredentialsFile) : string
118
    {
119
        $command = [
120
            "{$this->dumpBinaryPath}mysqldump",
121
            "--defaults-extra-file={$temporaryCredentialsFile}",
122
            '--skip-comments',
123
            $this->useExtendedInserts ? '--extended-insert' : '--skip-extended-insert',
124
        ];
125
126
        if ($this->socket != '') {
127
            $command[] = "--socket={$this->socket}";
128
        }
129
130
        $command[] = "{$this->dbName} > {$dumpFile}";
131
132
        return implode(' ', $command);
133
    }
134
135
    public function getContentsOfCredentialsFile() : string
136
    {
137
        $contents = [
138
            '[client]',
139
            "user = '{$this->userName}'",
140
            "password = '{$this->password}'",
141
            "host = '{$this->host}'",
142
            "port = '{$this->port}'",
143
        ];
144
145
        return implode(PHP_EOL, $contents);
146
    }
147
148
    protected function guardAgainstIncompleteCredentials()
149
    {
150
        foreach (['userName', 'dbName', 'host'] as $requiredProperty) {
151
            if (strlen($this->$requiredProperty) === 0) {
152
                throw CannotStartDump::emptyParameter($requiredProperty);
153
            }
154
        }
155
    }
156
}
157