MysqliConnection::getCurrentSchema()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 4.25

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 6
cts 8
cp 0.75
rs 9.7998
c 0
b 0
f 0
cc 4
nc 4
nop 0
crap 4.25
1
<?php
2
3
namespace Soluble\DbWrapper\Connection;
4
5
use Soluble\DbWrapper\Adapter\MysqliAdapter;
6
use Soluble\DbWrapper\Exception;
7
use mysqli;
8
9
class MysqliConnection implements ConnectionInterface
10
{
11
    /**
12
     * @var MysqliAdapter
13
     */
14
    protected $adapter;
15
16
    /**
17
     * @var \mysqli
18
     */
19
    protected $resource;
20
21
    /**
22
     * @param MysqliAdapter $adapter
23
     * @param \mysqli       $resource
24
     */
25 13
    public function __construct(MysqliAdapter $adapter, mysqli $resource)
26
    {
27 13
        $this->adapter = $adapter;
28 13
        $this->resource = $resource;
29 13
    }
30
31
    /**
32
     * {@inheritdoc}
33
     *
34
     * @return \mysqli
35
     */
36 2
    public function getResource()
37
    {
38 2
        return $this->resource;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 1
    public function getHost()
45
    {
46 1
        $infos = explode(' ', trim($this->resource->host_info));
47
48 1
        return strtolower($infos[0]);
49
    }
50
51
    /**
52
     * Return current schema/database name.
53
     *
54
     * @throws Exception\RuntimeException
55
     *
56
     * @return string|false
57
     */
58 3
    public function getCurrentSchema()
59
    {
60 3
        $query = 'SELECT DATABASE() as current_schema';
61
        try {
62 3
            $results = $this->adapter->query($query);
63 3
            if (count($results) == 0 || $results[0]['current_schema'] === null) {
64 3
                return false;
65
            }
66
        } catch (\Exception $e) {
67
            throw new Exception\RuntimeException($e->getMessage());
68
        }
69
70 2
        return $results[0]['current_schema'];
71
    }
72
}
73