Completed
Push — master ( 126036...3fa63d )
by Maik
07:56
created

OrmConnection   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 212
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 93.65%

Importance

Changes 7
Bugs 2 Features 0
Metric Value
wmc 20
c 7
b 2
f 0
lcom 1
cbo 5
dl 0
loc 212
ccs 59
cts 63
cp 0.9365
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A configure() 0 4 1
D parseOptions() 0 37 9
A getDbType() 0 7 2
A createConnection() 0 19 3
A getConnection() 0 8 2
A getSchema() 0 4 1
A getType() 0 4 1
A passivateConnection() 0 4 1
1
<?php
2
namespace Nkey\Caribu\Orm;
3
4
/**
5
 * Connection related functionality for the ORM
6
 *
7
 * This class is part of Caribu package
8
 *
9
 * @author Maik Greubel <[email protected]>
10
 */
11
trait OrmConnection
12
{
13
    /**
14
     * Include logging facility
15
     */
16
    use \Generics\Logger\LoggerTrait;
17
18
    /**
19
     * The database connection
20
     *
21
     * @var \PDO
22
     */
23
    private $connection = null;
24
25
    /**
26
     * The concrete database type
27
     *
28
     * @var string
29
     */
30
    private $type = null;
31
32
    /**
33
     * The database schema
34
     *
35
     * @var string
36
     */
37
    private $schema = null;
38
39
    /**
40
     * Database connection user
41
     *
42
     * @var string
43
     */
44
    private $user = null;
45
46
    /**
47
     * Database connection password
48
     *
49
     * @var string
50
     */
51
    private $password = null;
52
53
    /**
54
     * Database connection host
55
     *
56
     * @var string
57
     */
58
    private $host = null;
59
60
    /**
61
     * Database connection port
62
     *
63
     * @var int
64
     */
65
    private $port = null;
66
67
    /**
68
     * Embedded database file
69
     *
70
     * @var string
71
     */
72
    private $file = null;
73
74
    /**
75
     * Settings to use for connection
76
     *
77
     * @var array
78
     */
79
    private $settings = null;
80
81
    /**
82
     * Database type
83
     *
84
     * @var \Nkey\Caribu\Type\IType
85
     */
86
    private $dbType = null;
87
88
    /**
89
     * Configure the Orm
90
     *
91
     * @param array $options Various options to use for configuration. See documentation for details.
92
     */
93 34
    public static function configure($options = array())
94
    {
95 34
        self::parseOptions($options);
96 34
    }
97
98
    /**
99
     * Parse the options
100
     *
101
     * @param array $options The options to parse
102
     */
103 34
    private static function parseOptions($options)
104
    {
105 34
        foreach ($options as $option => $value) {
106 1
            switch ($option) {
107 34
                case 'type':
108 34
                    self::getInstance()->type = $value;
109 34
                    break;
110
111 33
                case 'schema':
112 10
                    self::getInstance()->schema = $value;
113 10
                    break;
114
115 33
                case 'user':
116 10
                    self::getInstance()->user = $value;
117 10
                    break;
118
119 33
                case 'password':
120 10
                    self::getInstance()->password = $value;
121 10
                    break;
122
123 33
                case 'host':
124 10
                    self::getInstance()->host = $value;
125 10
                    break;
126
127 24
                case 'port':
128 1
                    self::getInstance()->port = $value;
129 1
                    break;
130
131 23
                case 'file':
132 23
                    self::getInstance()->file = $value;
133 23
                    break;
134
135
                default:
136
                    self::getInstance()->settings[$option] = $value;
137
            }
138 34
        }
139 34
    }
140
141
    /**
142
     * Retrieve the database type
143
     *
144
     * @return \Nkey\Caribu\Type\IType
145
     */
146 34
    public function getDbType()
147
    {
148 34
        if (null == $this->dbType) {
149 34
            $this->dbType = \Nkey\Caribu\Type\TypeFactory::create($this);
150 32
        }
151 32
        return $this->dbType;
152
    }
153
154
    /**
155
     * Create a new database connection
156
     *
157
     * @throws OrmException
158
     */
159 33
    private function createConnection()
160
    {
161 33
        $dsn = $this->getDbType()->getDsn();
162
163 32
        $dsn = self::interpolate($dsn, array(
164 32
            'host' => $this->host,
165 32
            'port' => ($this->port ? $this->port : $this->dbType->getDefaultPort()),
166 32
            'schema' => $this->schema,
167 32
            'file' => $this->file
168 32
        ));
169
170
        try {
171 32
            $this->connection = new \PDO($dsn, $this->user, $this->password, $this->settings);
172 32
            $this->connection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
173 32
            $this->getLog()->info("New instance of PDO connection established");
174 32
        } catch (\PDOException $exception) {
175
            throw OrmException::fromPrevious($exception, $exception->getMessage(), $exception->getCode());
176
        }
177 32
    }
178
179
    /**
180
     * Retrieve the database connection
181
     *
182
     * @return \PDO The database connection
183
     *
184
     * @throws OrmException
185
     */
186 34
    public function getConnection()
187
    {
188 34
        if (null == $this->connection) {
189 33
            $this->createConnection();
190 32
        }
191
192 33
        return $this->connection;
193
    }
194
195
    /**
196
     * Retrieve the selected schema of the connection
197
     *
198
     * @return string The name of schema
199
     */
200 6
    public function getSchema()
201
    {
202 6
        return $this->schema;
203
    }
204
205
    /**
206
     * Retrieve the type of database
207
     *
208
     * @return string The type of database
209
     */
210 34
    public function getType()
211
    {
212 34
        return self::getInstance()->type;
213
    }
214
215
    /**
216
     * Detach connection
217
     */
218 33
    private function passivateConnection()
219
    {
220 33
        unset($this->connection);
221 33
    }
222
}
223