MysqlDumper   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 70
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
dl 0
loc 70
ccs 0
cts 35
cp 0
rs 10
c 0
b 0
f 0
wmc 6
lcom 1
cbo 1

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A dump() 0 16 2
A restore() 0 18 3
1
<?php
2
namespace Wandu\Database\Dumper;
3
4
use PDO;
5
use Symfony\Component\Process\Process;
6
use Wandu\Database\Contracts\DumperInterface;
7
8
class MysqlDumper implements DumperInterface
9
{
10
    /** @var string */
11
    protected $host;
12
    
13
    /** @var string */
14
    protected $username;
15
    
16
    /** @var string */
17
    protected $password;
18
    
19
    /** @var string */
20
    protected $database;
21
    
22
    /**
23
     * @param string $host
24
     * @param string $username
25
     * @param string $password
26
     * @param string $database
27
     */
28
    public function __construct($host, $username, $password, $database)
29
    {
30
        $this->host = $host;
31
        $this->username = $username;
32
        $this->password = $password;
33
        $this->database = $database;
34
    }
35
36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function dump($dumpTargetFile = null, array $ignoreTables = [])
40
    {
41
        // if you see the error message below, run "mysql_upgrade -u root -p --force" and restart mysqld.
42
        // mysqldump: Couldn't execute 'SHOW VARIABLES LIKE 'ndbinfo\_version'': SELECT
43
        // command denied to user 'dump'@'localhost' for table 'session_variables' (1142)
0 ignored issues
show
Unused Code Comprehensibility introduced by
37% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
44
        $cmd = "mysqldump -h{$this->host}";
45
        $cmd .= " -u{$this->username}";
46
        $cmd .= " -p{$this->password}";
47
        $cmd .= " {$this->database}";
48
        foreach ($ignoreTables as $table) {
49
            $cmd .= " --ignore-table={$this->database}.{$table}";
50
        }
51
        //  | sed 's$),($),\\n($g'
52
        $command = new Process("{$cmd} > {$dumpTargetFile}");
53
        $command->mustRun();
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function restore($dumpTargetFile, array $ignoreTables = [])
60
    {
61
        $connection = new Pdo(
62
            "mysql:host={$this->host};port=3306",
63
            $this->username,
64
            $this->password
65
        );
66
67
        $connection->query("USE {$this->database}");
68
        foreach ($connection->query('SHOW TABLES')->fetchAll(PDO::FETCH_ASSOC) as $table) {
69
            $table = array_pop($table);
70
            if (in_array($table, $ignoreTables)) continue;
71
            $connection->query("DROP TABLE `{$table}`")->execute();
72
        }
73
74
        $sql = file_get_contents($dumpTargetFile);
75
        $connection->prepare($sql)->execute();
76
    }
77
}
78