Test Setup Failed
Push — master ( f31609...d38329 )
by Jarrett
02:05
created

SearchReplaceDatabase::getAllTables()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 0
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;
0 ignored issues
show
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
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();
0 ignored issues
show
Bug introduced by
Constructors do not have meaningful return values, anything that is returned from here is discarded. Are you sure this is correct?
Loading history...
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