Failed Conditions
Pull Request — develop (#3590)
by Jonathan
61:34
created

Driver   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Test Coverage

Coverage 66.67%

Importance

Changes 0
Metric Value
wmc 7
eloc 27
dl 0
loc 63
ccs 14
cts 21
cp 0.6667
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A connect() 0 32 4
A constructPdoDsn() 0 10 3
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\Connection;
10
use Doctrine\DBAL\Driver\PDOConnection;
11
use Doctrine\DBAL\Driver\PDOException;
12
use Doctrine\DBAL\Platforms\SqlitePlatform;
13
use function array_merge;
14
15
/**
16
 * The PDO Sqlite driver.
17
 */
18
class Driver extends AbstractSQLiteDriver
19
{
20
    /** @var mixed[] */
21
    private $userDefinedFunctions = [
22
        'sqrt' => ['callback' => [SqlitePlatform::class, 'udfSqrt'], 'numArgs' => 1],
23
        'mod'  => ['callback' => [SqlitePlatform::class, 'udfMod'], 'numArgs' => 2],
24
        'locate'  => ['callback' => [SqlitePlatform::class, 'udfLocate'], 'numArgs' => -1],
25
    ];
26
27
    /**
28
     * {@inheritdoc}
29
     */
30 276
    public function connect(
31
        array $params,
32
        string $username = '',
33
        string $password = '',
34
        array $driverOptions = []
35
    ) : Connection {
36 276
        if (isset($driverOptions['userDefinedFunctions'])) {
37
            $this->userDefinedFunctions = array_merge(
38
                $this->userDefinedFunctions,
39
                $driverOptions['userDefinedFunctions']
40
            );
41
            unset($driverOptions['userDefinedFunctions']);
42
        }
43
44
        try {
45 276
            $connection = new PDOConnection(
46 276
                $this->constructPdoDsn($params),
47
                $username,
48
                $password,
49
                $driverOptions
50
            );
51
        } catch (PDOException $ex) {
52
            throw DBALException::driverException($this, $ex);
53
        }
54
55 276
        $pdo = $connection->getWrappedConnection();
56
57 276
        foreach ($this->userDefinedFunctions as $fn => $data) {
58 276
            $pdo->sqliteCreateFunction($fn, $data['callback'], $data['numArgs']);
59
        }
60
61 276
        return $connection;
62
    }
63
64
    /**
65
     * Constructs the Sqlite PDO DSN.
66
     *
67
     * @param mixed[] $params
68
     *
69
     * @return string The DSN.
70
     */
71 276
    private function constructPdoDsn(array $params) : string
72
    {
73 276
        $dsn = 'sqlite:';
74 276
        if (isset($params['path'])) {
75
            $dsn .= $params['path'];
76 276
        } elseif (isset($params['memory'])) {
77 276
            $dsn .= ':memory:';
78
        }
79
80 276
        return $dsn;
81
    }
82
}
83