MysqlDumpBackupProcessor::execute()   A
last analyzed

Complexity

Conditions 3
Paths 5

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 3
nc 5
nop 0
1
<?php
2
3
namespace LilyLabs\DBBackup;
4
5
use LilyLabs\DBBackup\Contracts\BackupFileNameGenerator;
6
use LilyLabs\DBBackup\Contracts\BackupProcessor;
7
use LilyLabs\DBBackup\Contracts\BackupFile;
8
use Exception;
9
use Symfony\Component\Process\Process;
10
use Symfony\Component\Process\Exception\ProcessFailedException;
11
12
/**
13
 * Ejecuta mysqldump para respaldar la base de datos indicada con ayuda de
14
 * symfony/process 
15
 * 
16
 * @package LilyLabs\DBBackup
17
 * @author Abraham Chávez
18
 */
19
class MysqlDumpBackupProcessor implements BackupProcessor
20
{
21
    private $db_name;
22
    private $backupFilenameGenerator;
23
24
25
    function __construct(string $db_name, BackupFileNameGenerator $backupFilenameGenerator)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
26
    {
27
        $this->db_name = $db_name;
28
        $this->backupFilenameGenerator = $backupFilenameGenerator;
29
    }
30
31
   	public function execute() : BackupFile
32
    {
33
    	$filepath = $this->getFilePath();
34
        $process = new Process("mysqldump --login-path=local {$this->db_name} --single-transaction > {$filepath}");
35
        try {
36
            $process->mustRun();
37
            return new DBBackupFile($filepath);
38
        } catch (ProcessFailedException $exception) {
39
            if (file_exists($filepath)) {
40
                unlink($filepath);
41
            }
42
            throw new Exception($process->getErrorOutput());
43
        }
44
    }
45
46
    private function getFilePath() : string
47
    {
48
    	return sys_get_temp_dir()."/{$this->backupFilenameGenerator->getName()}";
49
    }
50
}