Sqlite3::getConnection()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 3
eloc 8
nc 3
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Crossjoin\Browscap\Parser\Sqlite\Adapter;
5
6
use Crossjoin\Browscap\Exception\ParserConditionNotSatisfiedException;
7
use Crossjoin\Browscap\Exception\ParserConfigurationException;
8
use Crossjoin\Browscap\Exception\ParserRuntimeException;
9
10
/**
11
 * Class Sqlite3
12
 *
13
 * @package Crossjoin\Browscap\Parser\Sqlite\Adapter
14
 * @author Christoph Ziegenberg <[email protected]>
15
 * @link https://github.com/crossjoin/browscap
16
 */
17
class Sqlite3 extends AdapterAbstract implements AdapterInterface, AdapterFactoryInterface
18
{
19
    /**
20
     * @var \SQLite3
21
     */
22
    protected $connection;
23
24
    /**
25
     * Sqlite3 constructor.
26
     *
27
     * @inheritdoc
28
     *
29
     * @throws ParserConditionNotSatisfiedException
30
     */
31
    public function __construct(string $fileName)
32
    {
33
        if (!$this->checkConditions()) {
34
            throw new ParserConditionNotSatisfiedException('Sqlite3 extension missing.');
35
        }
36
37
        parent::__construct($fileName);
38
    }
39
40
    /**
41
     * @return bool
42
     */
43
    protected function checkConditions() : bool
44
    {
45
        return class_exists('\SQLite3');
46
    }
47
48
    /**
49
     * @return \SQLite3
50
     * @throws ParserConfigurationException
51
     */
52
    protected function getConnection() : \SQLite3
53
    {
54
        if ($this->connection === null) {
55
            try {
56
                $this->connection = new \SQLite3($this->getFileName());
57
            } catch (\Exception $e) {
58
                throw new ParserConfigurationException(
59
                    "Could not connect to database '" . $this->getFileName() . "'.", 0, $e
60
                );
61
            }
62
        }
63
64
        return $this->connection;
65
    }
66
67
    /**
68
     * @inheritdoc
69
     *
70
     * @throws ParserConfigurationException
71
     * @throws ParserRuntimeException
72
     */
73
    public function beginTransaction() : bool
74
    {
75
        $result = @$this->getConnection()->exec('BEGIN TRANSACTION');
76
77
        if ($result === false) {
78
            throw new ParserRuntimeException('Transaction could not be started.', 0);
79
        }
80
81
        return $result;
82
    }
83
84
    /**
85
     * @inheritdoc
86
     *
87
     * @throws ParserConfigurationException
88
     * @throws ParserRuntimeException
89
     */
90
    public function commitTransaction() : bool
91
    {
92
        $result = @$this->getConnection()->exec('COMMIT TRANSACTION');
93
94
        if ($result === false) {
95
            throw new ParserRuntimeException('Transaction could not be committed.', 0);
96
        }
97
98
        return $result;
99
    }
100
101
    /**
102
     * @inheritdoc
103
     *
104
     * @throws ParserConfigurationException
105
     * @throws ParserRuntimeException
106
     */
107
    public function query(string $statement) : array
108
    {
109
        $result = @$this->getConnection()->query($statement);
110
111
        if ($result === false) {
112
            throw new ParserRuntimeException('Statement could not be executed.', 0);
113
        }
114
115
        $results = [];
116
        while ($row = $result->fetchArray(\SQLITE3_ASSOC)) {
117
            $results[] = $row;
118
        }
119
120
        return $results;
121
    }
122
123
    /**
124
     * @inheritdoc
125
     *
126
     * @throws ParserConfigurationException
127
     * @throws ParserRuntimeException
128
     */
129
    public function prepare(string $statement) : PreparedStatementInterface
130
    {
131
        $preparedStatement = @$this->getConnection()->prepare($statement);
132
133
        if ($preparedStatement === false) {
134
            throw new ParserRuntimeException('Statement could not be prepared.', 0);
135
        }
136
137
        return new Sqlite3PreparedStatement($preparedStatement);
138
    }
139
140
    /**
141
     * @inheritdoc
142
     *
143
     * @throws ParserConfigurationException
144
     * @throws ParserRuntimeException
145
     */
146
    public function exec(string $statement) : bool
147
    {
148
        $result = @$this->getConnection()->exec($statement);
149
150
        if ($result === false) {
151
            throw new ParserRuntimeException('Statement could not be executed.', 0);
152
        }
153
154
        return $result;
155
    }
156
157
    public function __destruct()
158
    {
159
        $this->connection = null;
160
    }
161
}
162