PdoMysqlConnection::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
namespace Soluble\DbWrapper\Connection;
4
5
use Soluble\DbWrapper\Adapter\PdoMysqlAdapter;
6
use Soluble\DbWrapper\Exception;
7
use PDO;
8
9
class PdoMysqlConnection implements ConnectionInterface
10
{
11
    /**
12
     * @var PdoMysqlAdapter
13
     */
14
    protected $adapter;
15
16
    /**
17
     * @var PDO
18
     */
19
    protected $resource;
20
21
    /**
22
     * @param PdoMysqlAdapter $adapter
23
     * @param PDO             $resource
24
     */
25 13
    public function __construct(PdoMysqlAdapter $adapter, PDO $resource)
26
    {
27 13
        $this->adapter = $adapter;
28 13
        $this->resource = $resource;
29 13
    }
30
31
    /**
32
     * {@inheritdoc}
33
     *
34
     * @return PDO
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->getAttribute(PDO::ATTR_CONNECTION_STATUS)));
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 2
    public function getCurrentSchema()
59
    {
60 2
        $query = 'SELECT DATABASE() as current_schema';
61
        try {
62 2
            $results = $this->adapter->query($query);
63 2
            if (count($results) == 0 || $results[0]['current_schema'] === null) {
64 2
                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