Operator   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 122
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 122
ccs 42
cts 42
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 18 3
A exist() 0 4 1
A create() 0 6 2
A drop() 0 6 2
A copy() 0 6 1
A clear() 0 5 1
A exec() 0 13 1
1
<?php
2
/**
3
 * @author Sergii Bondarenko, <[email protected]>
4
 */
5
namespace Drupal\TqExtension\Utils\Database;
6
7
// Helpers.
8
use Behat\DebugExtension\Debugger;
9
10
/**
11
 * Class Operator.
12
 *
13
 * @package Drupal\TqExtension\Utils\Database
14
 */
15
class Operator
16
{
17
    use Debugger;
18
19
    /**
20
     * MySQL and MySQLDump login arguments.
21
     *
22
     * @var string
23
     */
24
    private $credentials = '';
25
26
    /**
27
     * Operator constructor.
28
     *
29
     * @param string $username
30
     *   The name of MySQL user.
31
     * @param string $password
32
     *   Password from an account of MySQL user.
33
     * @param string $host
34
     *   MySQL host address.
35
     * @param string $port
36
     *   Port of the MySQL host.
37
     */
38 24
    public function __construct($username, $password = '', $host = '', $port = '')
39
    {
40
        // -u%s -p%s -h%s -P%s
41 24
        $credentials = [];
42
43
        foreach ([
44 24
            'u' => $username,
45 24
            'p' => $password,
46 24
            'h' => $host,
47 24
            'P' => $port,
48 24
        ] as $parameter => $value) {
49 24
            if (!empty($value)) {
50 24
                $credentials[] = "-$parameter$value";
51 24
            }
52 24
        }
53
54 24
        $this->credentials = implode(' ', $credentials);
55 24
    }
56
57
    /**
58
     * @param string $name
59
     *   Name of the database to check.
60
     *
61
     * @return bool
62
     *   Checking state.
63
     */
64 20
    public function exist($name)
65
    {
66 20
        return !empty($this->exec("mysql -e 'show databases' | grep '^$name$'"));
67
    }
68
69
    /**
70
     * @param string $name
71
     *   Name of the database to create.
72
     */
73 16
    public function create($name)
74
    {
75 16
        if (!$this->exist($name)) {
76 16
            $this->exec("mysql -e '%s database $name;'", __FUNCTION__);
77 16
        }
78 16
    }
79
80
    /**
81
     * @param string $name
82
     *   Name of the database to drop.
83
     */
84 16
    public function drop($name)
85
    {
86 16
        if ($this->exist($name)) {
87 16
            $this->exec("mysql -e '%s database $name;'", __FUNCTION__);
88 16
        }
89 16
    }
90
91
    /**
92
     * @param string $source
93
     *   Source DB name.
94
     * @param string $destination
95
     *   Name of the new DB.
96
     */
97 8
    public function copy($source, $destination)
98
    {
99 8
        $this->drop($destination);
100 8
        $this->create($destination);
101 8
        $this->exec("mysqldump $source | mysql $destination");
102 8
    }
103
104
    /**
105
     * @param string $name
106
     *   Name of the DB.
107
     */
108 4
    public function clear($name)
109
    {
110 4
        $this->drop($name);
111 4
        $this->create($name);
112 4
    }
113
114
    /**
115
     * Executes a shell command.
116
     *
117
     * @param string $command
118
     *   Command to execute.
119
     *
120
     * @return string
121
     *   Result of a shell command.
122
     */
123 20
    private function exec($command)
124
    {
125
        // Adding credentials after "mysql" and "mysqldump" commands.
126 20
        $command = preg_replace(
127 20
            '/(mysql(?:dump)?)/',
128 20
            "\\1 $this->credentials",
129 20
            vsprintf($command, array_slice(func_get_args(), 1))
130 20
        );
131
132 20
        self::debug(['%s'], [$command]);
133
134 20
        return trim(shell_exec($command));
135
    }
136
}
137