MysqlDumpBackupProcessor   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 32
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 5
lcom 1
cbo 3
dl 0
loc 32
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A execute() 0 14 3
A getFilePath() 0 4 1
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
}