Completed
Pull Request — master (#3)
by
unknown
01:44
created

MySql::setHost()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
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 $socket = 0;
12
    protected $useExtendedInserts = true;
13
14
    /**
15
     * MySql constructor.
16
     */
17
    public function __construct()
18
    {
19
        $this->port = 3306;
20
    }
21
22
    /**
23
     * @param int $socket
24
     *
25
     * @return \Spatie\DbDumper\Databases\MySql
26
     */
27
    public function setSocket($socket)
28
    {
29
        $this->socket = $socket;
30
31
        return $this;
32
    }
33
34
    /**
35
     * @return \Spatie\DbDumper\Databases\MySql
36
     */
37
    public function useExtendedInserts()
38
    {
39
        $this->useExtendedInserts = true;
40
41
        return $this;
42
    }
43
44
    /**
45
     * @return \Spatie\DbDumper\Databases\MySql
46
     */
47
    public function dontUseExtendedInserts()
48
    {
49
        $this->useExtendedInserts = false;
50
51
        return $this;
52
    }
53
54
    /**
55
     * Dump the contents of the database to the given file.
56
     *
57
     * @param string $dumpFile
58
     *
59
     * @throws \Spatie\DbDumper\Exceptions\CannotStartDump
60
     * @throws \Spatie\DbDumper\Exceptions\DumpFailed
61
     */
62
    public function dumpToFile($dumpFile)
63
    {
64
        $this->guardAgainstIncompleteCredentials();
65
66
        $tempFileHandle = tmpfile();
67
68
        fwrite($tempFileHandle, $this->getContentsOfCredentialsFile());
69
70
        $temporaryCredentialsFile = stream_get_meta_data($tempFileHandle)['uri'];
71
72
        $command = $this->getDumpCommand($dumpFile, $temporaryCredentialsFile);
73
74
        $process = new Process($command);
75
76
        $process->run();
77
78
        $this->checkIfDumpWasSuccessFul($process, $dumpFile);
79
    }
80
81
    /**
82
     * Get the command that should be performed to dump the database.
83
     *
84
     * @param string $dumpFile
85
     * @param string $temporaryCredentialsFile
86
     *
87
     * @return string
88
     */
89
    public function getDumpCommand($dumpFile, $temporaryCredentialsFile)
90
    {
91
        $command = [
92
            "{$this->dumpBinaryPath}mysqldump",
93
            "--defaults-extra-file={$temporaryCredentialsFile}",
94
            '--skip-comments',
95
            $this->useExtendedInserts ? '--extended-insert' : '--skip-extended-insert',
96
        ];
97
98
        if ($this->socket > 0) {
99
            $command[] = "--socket={$this->socket}";
100
        }
101
102
        $command[] = "{$this->dbName} > {$dumpFile}";
103
104
        return implode(' ', $command);
105
    }
106
107
    /**
108
     * @return string
109
     */
110
    public function getContentsOfCredentialsFile()
111
    {
112
        $contents = [
113
            '[client]',
114
            "user = '{$this->userName}'",
115
            "password = '{$this->password}'",
116
            "host = '{$this->host}'",
117
            "port = '{$this->port}'",
118
        ];
119
120
        return implode(PHP_EOL, $contents);
121
    }
122
123 View Code Duplication
    protected function guardAgainstIncompleteCredentials()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
124
    {
125
        foreach (['userName', 'dbName', 'host'] as $requiredProperty) {
126
            if ($this->$requiredProperty === '') {
127
                throw CannotStartDump::emptyParameter($requiredProperty);
128
            }
129
        }
130
    }
131
}
132