Completed
Push — master ( 348a0e...d82154 )
by Maik
06:35
created

OrmConnection::createConnection()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 3.0327

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 19
ccs 11
cts 13
cp 0.8462
rs 9.4285
c 1
b 0
f 0
cc 3
eloc 13
nc 4
nop 0
crap 3.0327
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
92
     *            Various options to use for configuration. See documentation for details.
93
     */
94 34
    public static function configure($options = array())
95
    {
96 34
        self::parseOptions($options);
97 34
    }
98
99
    /**
100
     * Assign the option to local configuration
101
     *
102
     * @param string $option
103
     *            The option to assign
104
     * @param mixed $value
105
     *            The value to assign to option
106
     */
107 34
    private static function assignOption(string $option, $value)
108
    {
109
        switch ($option) {
110 34
            case 'type':
111 34
                self::getInstance()->type = $value;
112 34
                break;
113
            
114 33
            case 'schema':
115 10
                self::getInstance()->schema = $value;
116 10
                break;
117
            
118 33
            case 'user':
119 10
                self::getInstance()->user = $value;
120 10
                break;
121
            
122 33
            case 'password':
123 10
                self::getInstance()->password = $value;
124 10
                break;
125
            
126 33
            case 'host':
127 10
                self::getInstance()->host = $value;
128 10
                break;
129
            
130 24
            case 'port':
131 1
                self::getInstance()->port = $value;
132 1
                break;
133
            
134 23
            case 'file':
135 23
                self::getInstance()->file = $value;
136 23
                break;
137
            
138
            default:
139
                self::getInstance()->settings[$option] = $value;
140
        }
141 34
    }
142
143
    /**
144
     * Parse the options
145
     *
146
     * @param array $options
147
     *            The options to parse
148
     */
149 34
    private static function parseOptions($options)
150
    {
151 34
        foreach ($options as $option => $value) {
152 34
            self::assignOption($option, $value);
153
        }
154 34
    }
155
156
    /**
157
     * Retrieve the database type
158
     *
159
     * @return \Nkey\Caribu\Type\IType
160
     */
161 34
    public function getDbType()
162
    {
163 34
        if (null == $this->dbType) {
164 34
            $this->dbType = \Nkey\Caribu\Type\TypeFactory::create($this);
165
        }
166 32
        return $this->dbType;
167
    }
168
169
    /**
170
     * Create a new database connection
171
     *
172
     * @throws OrmException
173
     */
174 33
    private function createConnection()
175
    {
176 33
        $dsn = $this->getDbType()->getDsn();
177
        
178 32
        $dsn = self::interpolate($dsn, array(
179 32
            'host' => $this->host,
180 32
            'port' => ($this->port ? $this->port : $this->dbType->getDefaultPort()),
181 32
            'schema' => $this->schema,
182 32
            'file' => $this->file
183
        ));
184
        
185
        try {
186 32
            $this->connection = new \PDO($dsn, $this->user, $this->password, $this->settings);
187 32
            $this->connection->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
188 32
            $this->getLog()->info("New instance of PDO connection established");
189
        } catch (\PDOException $exception) {
190
            throw OrmException::fromPrevious($exception, $exception->getMessage(), $exception->getCode());
191
        }
192 32
    }
193
194
    /**
195
     * Retrieve the database connection
196
     *
197
     * @return \PDO The database connection
198
     *        
199
     * @throws OrmException
200
     */
201 34
    public function getConnection()
202
    {
203 34
        if (null == $this->connection) {
204 33
            $this->createConnection();
205
        }
206
        
207 33
        return $this->connection;
208
    }
209
210
    /**
211
     * Retrieve the selected schema of the connection
212
     *
213
     * @return string The name of schema
214
     */
215 6
    public function getSchema()
216
    {
217 6
        return $this->schema;
218
    }
219
220
    /**
221
     * Retrieve the type of database
222
     *
223
     * @return string The type of database
224
     */
225 34
    public function getType()
226
    {
227 34
        return self::getInstance()->type;
228
    }
229
230
    /**
231
     * Detach connection
232
     */
233 33
    private function passivateConnection()
234
    {
235 33
        unset($this->connection);
236 33
    }
237
}
238