Failed Conditions
Push — drop-deprecated ( d77510...d983c4 )
by Michael
40:59 queued 38:23
created

Driver::connect()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5.2596

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 28
ccs 8
cts 14
cp 0.5714
rs 9.7
c 0
b 0
f 0
cc 4
nc 6
nop 4
crap 5.2596
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\Driver\PDOSqlite;
6
7
use Doctrine\DBAL\DBALException;
8
use Doctrine\DBAL\Driver\AbstractSQLiteDriver;
9
use Doctrine\DBAL\Driver\PDOConnection;
10
use Doctrine\DBAL\Driver\PDOException;
11
use Doctrine\DBAL\Platforms\SqlitePlatform;
12
use function array_merge;
13
14
/**
15
 * The PDO Sqlite driver.
16
 */
17
class Driver extends AbstractSQLiteDriver
18
{
19
    /** @var mixed[] */
20
    protected $_userDefinedFunctions = [
21
        'sqrt' => ['callback' => [SqlitePlatform::class, 'udfSqrt'], 'numArgs' => 1],
22
        'mod'  => ['callback' => [SqlitePlatform::class, 'udfMod'], 'numArgs' => 2],
23
        'locate'  => ['callback' => [SqlitePlatform::class, 'udfLocate'], 'numArgs' => -1],
24
    ];
25
26
    /**
27
     * {@inheritdoc}
28
     */
29 368
    public function connect(array $params, $username = null, $password = null, array $driverOptions = [])
30
    {
31 368
        if (isset($driverOptions['userDefinedFunctions'])) {
32
            $this->_userDefinedFunctions = array_merge(
33
                $this->_userDefinedFunctions,
34
                $driverOptions['userDefinedFunctions']
35
            );
36
            unset($driverOptions['userDefinedFunctions']);
37
        }
38
39
        try {
40 368
            $connection = new PDOConnection(
41 368
                $this->_constructPdoDsn($params),
42
                $username,
43
                $password,
44
                $driverOptions
45
            );
46
        } catch (PDOException $ex) {
47
            throw DBALException::driverException($this, $ex);
48
        }
49
50 368
        $pdo = $connection->getWrappedConnection();
51
52 368
        foreach ($this->_userDefinedFunctions as $fn => $data) {
53 368
            $pdo->sqliteCreateFunction($fn, $data['callback'], $data['numArgs']);
54
        }
55
56 368
        return $connection;
57
    }
58
59
    /**
60
     * Constructs the Sqlite PDO DSN.
61
     *
62
     * @param mixed[] $params
63
     *
64
     * @return string The DSN.
65
     */
66 368
    protected function _constructPdoDsn(array $params)
67
    {
68 368
        $dsn = 'sqlite:';
69 368
        if (isset($params['path'])) {
70
            $dsn .= $params['path'];
71 368
        } elseif (isset($params['memory'])) {
72 368
            $dsn .= ':memory:';
73
        }
74
75 368
        return $dsn;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81 253
    public function getName()
82
    {
83 253
        return 'pdo_sqlite';
84
    }
85
}
86