Completed
Push — master ( 066f50...d2dc6a )
by Sébastien
01:55
created

AdapterFactory   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 87.5%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 4
c 2
b 1
f 0
lcom 0
cbo 2
dl 0
loc 30
ccs 14
cts 16
cp 0.875
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A createAdapterFromConnection() 0 19 4
1
<?php
2
3
namespace Soluble\DbWrapper;
4
5
class AdapterFactory
6
{
7
8
    /**
9
     * Get an adapter from an existing connection
10
     *
11
     * @param \PDO|\mysqli $connection database connection object
12
     * @throws Exception\InvalidArgumentException
13
     * @return \Soluble\DbWrapper\Adapter\MysqlAdapter
14
     */
15 3
    public static function createAdapterFromConnection($connection)
16
    {
17 3
        if ($connection instanceof \PDO) {
18 2
            switch ($connection->getAttribute(\PDO::ATTR_DRIVER_NAME)) {
19 2
                case 'mysql':
20 1
                    $adapter = new Adapter\MysqlAdapter($connection);
21 1
                    break;
22 1
                default:
23 1
                    $msg = "Currently only support 'pdo_mysql' connection";
24 1
                    throw new Exception\InvalidArgumentException($msg);
25 2
            }
26 2
        } elseif ($connection instanceof \mysqli) {
27 1
            $adapter = new Adapter\MysqlAdapter($connection);
28 1
        } else {
29
            $msg = "Currently only support 'pdo' or 'mysqli' connections";
30
            throw new Exception\InvalidArgumentException($msg);
31
        }
32 2
        return $adapter;
33
    }
34
}
35