AdapterFactory::createAdapterFromResource()   A
last analyzed

Complexity

Conditions 6
Paths 4

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6

Importance

Changes 0
Metric Value
dl 0
loc 15
ccs 9
cts 9
cp 1
rs 9.2222
c 0
b 0
f 0
cc 6
nc 4
nop 1
crap 6
1
<?php
2
3
namespace Soluble\DbWrapper;
4
5
class AdapterFactory
6
{
7
    /**
8
     * Create adapter from an existing doctrine/dbal connection.
9
     *
10
     * @param \Illuminate\Database\Capsule\Manager $capsule
11
     *
12
     * @return \Soluble\DbWrapper\Adapter\Laravel\Capsule5Adapter
13
     */
14 1
    public static function createAdapterFromCapsule5(\Illuminate\Database\Capsule\Manager $capsule)
15
    {
16 1
        return new \Soluble\DbWrapper\Adapter\Laravel\Capsule5Adapter($capsule);
17
    }
18
19
    /**
20
     * Create adapter from an existing doctrine/dbal connection.
21
     *
22
     * @param \Doctrine\DBAL\Connection $dbalConnection
23
     *
24
     * @return \Soluble\DbWrapper\Adapter\Doctrine\Dbal2Adapter
25
     */
26 1
    public static function createAdapterFromDoctrine2(\Doctrine\DBAL\Connection $dbalConnection)
27
    {
28 1
        return new \Soluble\DbWrapper\Adapter\Doctrine\Dbal2Adapter($dbalConnection);
29
    }
30
31
    /**
32
     * Create adapter from an existing zendframework/zend-db adapter.
33
     *
34
     * @param \Zend\Db\Adapter\Adapter $zendAdapter
35
     *
36
     * @return \Soluble\DbWrapper\Adapter\Zend\ZendDb2Adapter
37
     */
38 1
    public static function createAdapterFromZendDb2(\Zend\Db\Adapter\Adapter $zendAdapter)
39
    {
40 1
        return new \Soluble\DbWrapper\Adapter\Zend\ZendDb2Adapter($zendAdapter);
41
    }
42
43
    /**
44
     * Create adapter from an existing connection resource.
45
     *
46
     * @param mixed $resource database connection object (mysqli, pdo_mysql,...)
47
     *
48
     * @throws Exception\InvalidArgumentException
49
     * @throws Exception\UnsupportedDriverException
50
     *
51
     * @return Adapter\AdapterInterface
52
     */
53 5
    public static function createAdapterFromResource($resource)
54
    {
55 5
        if (is_scalar($resource) || is_array($resource)) {
56 1
            throw new Exception\InvalidArgumentException("Resource param must be a valid 'resource' link (mysqli, pdo)");
57
        }
58 4
        if ($resource instanceof \PDO) {
59 2
            $adapter = self::getAdapterFromPdo($resource);
60 2
        } elseif (extension_loaded('mysqli') && $resource instanceof \mysqli) {
61 1
            $adapter = new Adapter\MysqliAdapter($resource);
62
        } else {
63 1
            throw new Exception\InvalidArgumentException('Resource must be a valid connection link, like PDO or mysqli');
64
        }
65
66 3
        return $adapter;
67
    }
68
69
    /**
70
     * Get an adapter from an existing connection resource.
71
     *
72
     * @param \PDO $resource database connection object
73
     *
74
     * @throws Exception\UnsupportedDriverException
75
     *
76
     * @return Adapter\AdapterInterface
77
     */
78 2
    protected static function getAdapterFromPdo(\PDO $resource)
79
    {
80 2
        $driver = strtolower($resource->getAttribute(\PDO::ATTR_DRIVER_NAME));
81
        switch ($driver) {
82 2
            case 'mysql':
83 1
                $adapter = new Adapter\PdoMysqlAdapter($resource);
84 1
                break;
85 1
            case 'sqlite':
86 1
                $adapter = new Adapter\PdoSqliteAdapter($resource);
87 1
                break;
88
            default:
89
                $msg = "Driver 'PDO_$driver' is not currently supported.";
90
                throw new Exception\UnsupportedDriverException($msg);
91
        }
92
93 2
        return $adapter;
94
    }
95
}
96