Completed
Push — master ( a2c400...8a7993 )
by Nikola
09:09
created

MySqlDumpSource   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 98
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 84.38%

Importance

Changes 4
Bugs 0 Features 2
Metric Value
wmc 9
c 4
b 0
f 2
lcom 1
cbo 7
dl 0
loc 98
ccs 27
cts 32
cp 0.8438
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
B fetch() 0 25 4
B buildProcess() 0 25 4
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\SourceInterface;
18
use RunOpenCode\Backup\Event\BackupEvents;
19
use RunOpenCode\Backup\Event\EventDispatcherAwareTrait;
20
use RunOpenCode\Backup\Exception\SourceException;
21
use RunOpenCode\Backup\Utils\Filename;
22
use Symfony\Component\Process\ProcessBuilder;
23
24
/**
25
 * Class MySqlDumpSource
26
 *
27
 * Fetch database dump for backup.
28
 *
29
 * @package RunOpenCode\Backup\Source
30
 */
31
class MySqlDumpSource implements SourceInterface, EventDispatcherAwareInterface
32
{
33
    use EventDispatcherAwareTrait;
34
35
    /**
36
     * @var string
37
     */
38
    protected $database;
39
40
    /**
41
     * @var string
42
     */
43
    protected $user;
44
45
    /**
46
     * @var string
47
     */
48
    protected $password;
49
50
    /**
51
     * @var string
52
     */
53
    protected $host;
54
55
    /**
56
     * @var int
57
     */
58
    protected $port;
59 6
60
    public function __construct($database, $user, $password = null, $host = null, $port = 3306)
61 6
    {
62 6
        $this->database = $database;
63 6
        $this->user = $user;
64 6
        $this->password = $password;
65 6
        $this->host = $host;
66 6
        $this->port = $port;
67
    }
68
69
    /**
70
     * {@inheritdoc}
71 6
     */
72
    public function fetch()
73 6
    {
74
        $process = $this->buildProcess();
75
76 6
        $process->run();
77 6
78
        if (!$process->isSuccessful()) {
79 6
            throw new SourceException(sprintf('Unable to dump MySql database "%s", reason: "%s".', $this->database, $process->getErrorOutput()));
80
        }
81 4
82
        $mySqlDump = Filename::temporaryFile(sprintf('mysql-dump-%s-%s-%s.sql', $this->database, (is_null($this->host) ? 'localhost' : $this->host), date('Y-m-d-H-i-s')));
83
84 4
        if (file_put_contents($mySqlDump, $process->getOutput()) === false) {
85
86 4
            throw new \RuntimeException(sprintf('Unable to save MySql dump of database into "%s".', $mySqlDump));
87
88 6
        } else {
89
90
            $this->getEventDispatcher()->addListener(BackupEvents::TERMINATE, function() use ($mySqlDump) {
91
                unlink($mySqlDump);
92 6
            });
93
94 6
            return array(File::fromLocal($mySqlDump, dirname($mySqlDump)));
95
        }
96 6
    }
97
98 6
    /**
99 4
     * Builds mysqldump process.
100
     *
101
     * @return \Symfony\Component\Process\Process
102 2
     */
103
    protected function buildProcess()
104 2
    {
105
        $processBuilder = new ProcessBuilder();
106
107
        $processBuilder
108
            ->add('mysqldump')
109
            ->add(sprintf('-u%s', $this->user));
110 2
111 2
        if (!is_null($this->host)) {
112 2
113
            if (!is_null($this->port)) {
114 2
                $processBuilder->add(sprintf('-h%s:%s', $this->host, $this->port));
115
            } else {
116
                $processBuilder->add(sprintf('-h%s', $this->host, $this->port));
117
            }
118
        }
119
120
        if (null !== $this->password) {
121
            $processBuilder->add(sprintf('-p%s', $this->password));
122
        }
123
124
        $processBuilder->add($this->database);
125
126
        return $processBuilder->getProcess();
127
    }
128
}
129