Client::getConnectionString()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
/**
3
 * @maintainer Timur Shagiakhmetov <[email protected]>
4
 */
5
6
namespace Shagtv\DBAL;
7
8
use Doctrine\DBAL\Configuration;
9
use Doctrine\DBAL\Connection;
10
use Doctrine\DBAL\DBALException;
11
use Doctrine\DBAL\DriverManager;
12
13
class Client
14
{
15
    /** @var Connection */
16
    protected $Conn;
17
    /** @var string */
18
    protected $connection_string;
19
20
    public function __construct($connection_string = '')
21
    {
22
        $this->connection_string = $connection_string;
23
    }
24
25
    /**
26
     * @return Connection
27
     * @throws DBALException
28
     */
29
    public function getConnection()
30
    {
31
        if (null === $this->Conn) {
32
            $config = new Configuration();
33
            $connectionParams = ['url' => $this->connection_string];
34
            $this->Conn = DriverManager::getConnection($connectionParams, $config);
35
        }
36
37
        return $this->Conn;
38
    }
39
40
    /**
41
     * @param Connection $Conn
42
     * @return $this
43
     */
44
    public function setConnection(Connection $Conn)
45
    {
46
        $this->Conn = $Conn;
47
        return $this;
48
    }
49
50
    /**
51
     * @param string $connection_string
52
     * @return $this
53
     */
54
    public function setConnectionString($connection_string)
55
    {
56
        $this->connection_string = $connection_string;
57
        return $this;
58
    }
59
60
    /**
61
     * @return string
62
     */
63
    public function getConnectionString()
64
    {
65
        return $this->connection_string;
66
    }
67
68
    /**
69
     * @throws DBALException
70
     */
71
    public function createTable()
72
    {
73
        $sql = '
74
            CREATE TABLE IF NOT EXISTS records (
75
              id INTEGER PRIMARY KEY AUTOINCREMENT,
76
              name TEXT DEFAULT NULL
77
            );
78
        ';
79
80
        $this->getConnection()->exec($sql);
81
        return true;
82
    }
83
}
84