Completed
Push — master ( f1bafc...dbcbc1 )
by Sébastien
03:15
created

src/Soluble/DbWrapper/AdapterFactory.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Soluble\DbWrapper;
4
5
class AdapterFactory
6
{
7
8
    /**
9
     * Create adapter from an existing connection resource
10
     *
11
     * @param mixed $resource database connection object (mysqli, pdo_mysql,...)
12
     * @throws Exception\InvalidArgumentException
13
     * @throws Exception\UnsupportedDriverException
14
     * @return Adapter\AdapterInterface
15
     */
16 3
    public static function createAdapterFromResource($resource)
17
    {
18 3
        if (is_scalar($resource) || is_array($resource)) {
19
           throw new Exception\InvalidArgumentException("Resource param must be a valid 'resource' link (mysqli, pdo)");
0 ignored issues
show
It seems like the identation of this line is off (expected at least 12 spaces, but found 11).
Loading history...
20 3
        } if ($resource instanceof \PDO) {
21 2
            $adapter = self::getAdapterFromPdo($resource);
22 2
        } elseif (extension_loaded('mysqli') && $resource instanceof \mysqli) {
23 1
            $adapter = new Adapter\MysqliAdapter($resource);
24 1
        } elseif (is_object($resource)) {
25
            $class = get_class($resource);
26
            $msg = "Resource connection '$class' is either unsupported or php extension not enabled.";
27
            throw new Exception\UnsupportedDriverException($msg);
28
        } else {
29
            throw new Exception\InvalidArgumentException("Resource must be a valid connection link, like PDO or mysqli");
30
        }
31 2
        return $adapter;
32
    }
33
    
34
 
35
    /**
36
     * Get an adapter from an existing connection resource
37
     *
38
     * @param \PDO $resource database connection object
39
     * @throws Exception\UnsupportedDriverException
40
     * @return Adapter\AdapterInterface
41
     */
42 2
    protected static function getAdapterFromPdo(\PDO $resource) {
43
        
44 2
        $driver = $resource->getAttribute(\PDO::ATTR_DRIVER_NAME);
45
        switch ($driver) {
46 2
            case 'mysql':
47 1
                $adapter = new Adapter\PdoMysqlAdapter($resource);
48 1
                break;
49 1
            default:
50 1
                $msg = "Driver 'PDO_$driver' is not currently supported.";
51 1
                throw new Exception\UnsupportedDriverException($msg);
52 1
        }
53 1
        return $adapter;
54
    }
55
}
56