SearchReplaceDatabase   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 57
rs 10
wmc 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getDatabase() 0 3 1
A db() 0 9 2
A getAllTables() 0 5 1
A __construct() 0 8 1
A useDatabase() 0 10 2
1
<?php namespace SearchReplace;
2
3
use SearchReplace\SearchReplaceDatabaseInterface;
4
use SearchReplace\SearchReplaceException as Exception;
5
6
/**
7
 * Class SearchReplaceDatabase
8
 * @package SearchReplace
9
 * @author Jarrett Barnett <[email protected]>
10
 */
11
class SearchReplaceDatabase implements SearchReplaceDatabaseInterface
12
{
13
    protected $db, $host, $username, $password, $database;
14
15
    public function __construct($host, $username, $password, $database)
16
    {
17
        $this->host = $host;
18
        $this->username = $username;
19
        $this->password = $password;
20
        $this->database = $database;
21
22
        return $this->db();
23
    }
24
    
25
    /**
26
     * Return db instance
27
     *
28
     * @return mixed
29
     * @throws SearchReplaceException
30
     */
31
    public function db()
32
    {
33
        $this->db = new \mysqli($this->host, $this->username, $this->password, $this->database);
34
35
        if ($this->db->connect_errno) {
36
            throw new Exception("Failed to connect to MySQL: (" . $this->db->connect_errno . ") " . $this->db->connect_error);
37
        }
38
39
        return $this;
40
    }
41
    
42
    public function getDatabase()
43
    {
44
        return $this->db;
45
    }
46
    
47
    public function useDatabase()
48
    {
49
        if (empty($this->database))
50
        {
51
            return false;
52
        }
53
        
54
        ($this->db)::select_db($this->database);
55
        
56
        return $this;
57
    }
58
    
59
    /**
60
     * Get All Tables
61
     * @return mixed
62
     */
63
    public function getAllTables()
64
    {
65
        $result = $this->db->query('SHOW TABLES');
66
        
67
        return $result->toArray();
68
    }
69
}
70