Completed
Push — master ( f105fc...3abe44 )
by Sergii
02:30
created

DatabaseManager::__destruct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * @author Sergey Bondarenko, <[email protected]>
4
 */
5
namespace Drupal\TqExtension\Utils;
6
7
class DatabaseManager
8
{
9
    /**
10
     * @var string
11
     */
12
    private $credentials = '-u%s -p%s';
13
    /**
14
     * Name of original database.
15
     *
16
     * @var string
17
     */
18
    private $originalName = '';
19
    /**
20
     * Name of temporary database that will store data from original.
21
     *
22
     * @var string
23
     */
24
    private $newName = '';
25
    /**
26
     * Name of an object where this class is called.
27
     *
28
     * @var string
29
     */
30
    private $callee;
31
32
    /**
33
     * @param string $connection
34
     *   Database connection name (key in $databases array from settings.php).
35
     * @param string $callee
36
     *   Must be the value of "self::class" of callee object.
37
     */
38
    public function __construct($connection, $callee)
39
    {
40
        if (!defined('DRUPAL_ROOT') || !function_exists('conf_path')) {
41
            throw new \RuntimeException('Drupal is not bootstrapped.');
42
        }
43
44
        if (!class_exists($callee)) {
45
            throw new \InvalidArgumentException(sprintf('An object of "%s" type does not exist.', $callee));
46
        }
47
48
        /** @var array $databases */
49
        require sprintf('%s/%s/settings.php', DRUPAL_ROOT, conf_path());
50
51
        if (empty($databases[$connection])) {
0 ignored issues
show
Bug introduced by
The variable $databases seems to never exist, and therefore empty should always return true. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
52
            throw new \InvalidArgumentException(sprintf('The "%s" database connection does not exist.', $connection));
53
        }
54
55
        $info = $databases[$connection]['default'];
56
57
        $this->callee = $callee;
58
        $this->originalName = $info['database'];
59
        $this->newName = "tqextension_$this->originalName";
60
        $this->credentials = sprintf($this->credentials, $info['username'], $info['password']);
61
62
        foreach (['drop', 'create'] as $action) {
63
            $this->exec("mysql $this->credentials -e '$action database $this->newName;'");
64
        }
65
66
        $this->exec("mysqldump $this->credentials $this->originalName | mysql $this->credentials $this->newName");
67
    }
68
69
    /**
70
     * Restore original database.
71
     */
72
    public function __destruct()
73
    {
74
        $this->exec("mysqldump $this->credentials $this->newName | mysql $this->credentials $this->originalName");
75
    }
76
77
    /**
78
     * Executes a shell command.
79
     *
80
     * @param string $command
81
     */
82
    private function exec($command)
83
    {
84
        $command = vsprintf($command, array_slice(func_get_args(), 1));
85
86
        if (method_exists($this->callee, 'debug')) {
87
            call_user_func([$this->callee, 'debug'], [$command]);
88
        }
89
90
        shell_exec($command);
91
    }
92
}
93