Passed
Push — master ( 72d254...356a09 )
by Jean Paul
01:39
created

SqliteConnector::insert()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 12
c 1
b 0
f 0
dl 0
loc 19
rs 9.5555
cc 5
nc 4
nop 1
1
<?php
2
3
namespace Coco\SourceWatcher\Core\Database\Connections;
4
5
use Coco\SourceWatcher\Core\SourceWatcherException;
6
use Doctrine\DBAL\Connection;
7
use Doctrine\DBAL\DBALException;
8
use Doctrine\DBAL\DriverManager;
9
10
/**
11
 * Class SqliteConnector
12
 * @package Coco\SourceWatcher\Core\Database\Connections
13
 *
14
 * Following the definition from: https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#pdo-sqlite
15
 */
16
class SqliteConnector extends EmbeddedDatabaseConnector
17
{
18
    /**
19
     * @var string
20
     */
21
    protected string $path;
22
23
    /**
24
     * @var bool
25
     */
26
    protected bool $memory;
27
28
    /**
29
     * SqliteConnector constructor.
30
     */
31
    public function __construct ()
32
    {
33
        $this->driver = "pdo_sqlite";
34
35
        $this->memory = false;
36
    }
37
38
    /**
39
     * @return string
40
     */
41
    public function getPath () : string
42
    {
43
        return $this->path;
44
    }
45
46
    /**
47
     * @param string $path
48
     */
49
    public function setPath ( string $path ) : void
50
    {
51
        $this->path = $path;
52
    }
53
54
    /**
55
     * @return bool
56
     */
57
    public function isMemory () : bool
58
    {
59
        return $this->memory;
60
    }
61
62
    /**
63
     * @param bool $memory
64
     */
65
    public function setMemory ( bool $memory ) : void
66
    {
67
        $this->memory = $memory;
68
    }
69
70
    /**
71
     * @return Connection
72
     * @throws SourceWatcherException
73
     */
74
    public function connect () : Connection
75
    {
76
        try {
77
            return DriverManager::getConnection( $this->getConnectionParameters() );
78
        } catch ( DBALException $dbalException ) {
79
            throw new SourceWatcherException( "Something went wrong trying to get a connection: " . $dbalException->getMessage() );
80
        }
81
    }
82
83
    /**
84
     * @return array
85
     */
86
    protected function getConnectionParameters () : array
87
    {
88
        $this->connectionParameters = array();
89
90
        $this->connectionParameters["driver"] = $this->driver;
91
        $this->connectionParameters["user"] = $this->user;
92
        $this->connectionParameters["password"] = $this->password;
93
94
        if ( isset( $this->path ) && $this->path !== "" ) {
95
            $this->connectionParameters["path"] = $this->path;
96
        }
97
98
        $this->connectionParameters["memory"] = $this->memory;
99
100
        return $this->connectionParameters;
101
    }
102
}
103