Completed
Push — master ( 6e1259...c4a3c9 )
by Nikola
02:37
created

MySqlDumpSource::fetch()   B

Complexity

Conditions 5
Paths 12

Size

Total Lines 45
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 45
rs 8.439
cc 5
eloc 24
nc 12
nop 0
1
<?php
2
/*
3
 * This file is part of the Backup package, an RunOpenCode project.
4
 *
5
 * (c) 2015 RunOpenCode
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * This project is fork of "kbond/php-backup", for full credits info, please
11
 * view CREDITS file that was distributed with this source code.
12
 */
13
namespace RunOpenCode\Backup\Source;
14
15
use RunOpenCode\Backup\Backup\File;
16
use RunOpenCode\Backup\Contract\EventDispatcherAwareInterface;
17
use RunOpenCode\Backup\Contract\LoggerAwareInterface;
18
use RunOpenCode\Backup\Contract\SourceInterface;
19
use RunOpenCode\Backup\Event\BackupEvent;
20
use RunOpenCode\Backup\Event\EventDispatcherAwareTrait;
21
use RunOpenCode\Backup\Exception\SourceException;
22
use RunOpenCode\Backup\Log\LoggerAwareTrait;
23
use Symfony\Component\Process\ProcessBuilder;
24
25
/**
26
 * Class MySqlDumpSource
27
 *
28
 * Fetch database dump for backup.
29
 *
30
 * @package RunOpenCode\Backup\Source
31
 */
32
class MySqlDumpSource implements SourceInterface, LoggerAwareInterface, EventDispatcherAwareInterface
33
{
34
    use LoggerAwareTrait;
35
    use EventDispatcherAwareTrait;
36
37
    /**
38
     * @var string
39
     */
40
    protected $database;
41
42
    /**
43
     * @var string
44
     */
45
    protected $user;
46
47
    /**
48
     * @var string
49
     */
50
    protected $password;
51
52
    /**
53
     * @var string
54
     */
55
    protected $host;
56
57
    /**
58
     * @var int
59
     */
60
    protected $port;
61
62
    public function __construct($database, $user, $password = null, $host = null, $port = 3306)
63
    {
64
        $this->database = $database;
65
        $this->user = $user;
66
        $this->password = $password;
67
        $this->host = $host;
68
        $this->port = $port;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function fetch()
75
    {
76
        $processBuilder = new ProcessBuilder();
77
78
        $processBuilder
79
            ->add('mysqldump')
80
            ->add(sprintf('-u%s', $this->user));
81
82
        if (!is_null($this->host)) {
83
            $processBuilder->add(sprintf('-h%s:%s', $this->host, $this->port));
84
        }
85
86
        if (null !== $this->password) {
87
            $processBuilder->add(sprintf('-p%s', $this->password));
88
        }
89
90
        $processBuilder->add($this->database);
91
92
        $process = $processBuilder->getProcess();
93
94
        $process->run();
95
96
        if (!$process->isSuccessful()) {
97
            $this->getLogger()->error(sprintf('Unable to dump MySql database "%s".', $this->database, array(
98
                'process_error_output' => $process->getErrorOutput()
99
            )));
100
            throw new SourceException();
101
        }
102
103
        $tmpFile = tempnam(sys_get_temp_dir(), preg_replace('/[^a-zA-Z0-9-_\.]/','', sprintf('mysql-dump-%s-%s', $this->database, $this->host)));
104
105
        if (@file_put_contents($tmpFile, $process->getOutput()) === false) {
106
107
            $this->getLogger()->error(sprintf('Unable to save MySql dump of database into "%s".', $tmpFile));
108
            throw new \RuntimeException();
109
110
        } else {
111
112
            $this->getEventDispatcher()->addListener(BackupEvent::TERMINATE, function() use ($tmpFile) {
113
                @unlink($tmpFile);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
114
            });
115
116
            return array(File::fromLocal($tmpFile, dirname($tmpFile), sprintf('mysql-dump-%s-%s-%s.sql', $this->database, $this->host, date('Y-m-d-H-i-s'))));
117
        }
118
    }
119
}