Pdo::exec()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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