PdoConfiguration::setFetchMode()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * PdoConfigurationConfiguration.php
4
 * @author: [email protected]
5
 */
6
7
namespace FMUP\Db\Driver;
8
9
use FMUP\Db\DbInterface;
10
use FMUP\Db\Exception;
11
use FMUP\Logger;
12
13
abstract class PdoConfiguration implements DbInterface, Logger\LoggerInterface
14
{
15
    use Logger\LoggerTrait;
16
17
    private $settings = array();
18
    private $fetchMode = \PDO::FETCH_ASSOC;
19
    private $instance = null;
20
21
    /**
22
     * @return string
23
     */
24 3
    protected function getLoggerName()
25
    {
26 3
        return Logger\Channel\System::NAME;
27
    }
28
29
    /**
30
     * @param array $params
31
     */
32 44
    public function __construct($params = array())
33
    {
34 44
        $this->settings = $params;
35 44
    }
36
37
    /**
38
     * Database to connect to
39
     * @return string|null
40
     */
41 4
    protected function getDatabase()
42
    {
43 4
        return isset($this->settings['database']) ? $this->settings['database'] : null;
44
    }
45
46
    /**
47
     * Host to connect to
48
     * @return string
49
     */
50 6
    protected function getHost()
51
    {
52 6
        return isset($this->settings['host']) ? $this->settings['host'] : 'localhost';
53
    }
54
55
    /**
56
     * Dsn Driver to use
57
     * @return string
58
     */
59 5
    protected function getDsnDriver()
60
    {
61 5
        return isset($this->settings['driver']) ? $this->settings['driver'] : 'mysql';
62
    }
63
64
    /**
65
     * Retrieve settings
66
     * @param null $param
67
     * @return array|null
68
     */
69 7
    protected function getSettings($param = null)
70
    {
71 7
        return is_null($param) ? $this->settings : (isset($this->settings[$param]) ? $this->settings[$param] : null);
72
    }
73
74
    /**
75
     * Options for PDO Settings
76
     * @return array
77
     */
78 7
    protected function getOptions()
79
    {
80
        return array(
81 7
            \PDO::ATTR_PERSISTENT => (bool)(
82 7
            isset($this->settings['PDOBddPersistant']) ? $this->settings['PDOBddPersistant'] : false
83
            ),
84 7
            \PDO::ATTR_EMULATE_PREPARES => true,
85 7
            \PDO::MYSQL_ATTR_LOCAL_INFILE => true,
86
        );
87
    }
88
89
    /**
90
     * Login to use
91
     * @return string
92
     */
93 6
    protected function getLogin()
94
    {
95 6
        return isset($this->settings['login']) ? $this->settings['login'] : '';
96
    }
97
98
    /**
99
     * Password to use
100
     * @return string
101
     */
102 6
    protected function getPassword()
103
    {
104 6
        return isset($this->settings['password']) ? $this->settings['password'] : '';
105
    }
106
107
    /**
108
     * Get string for dsn construction
109
     * @return string
110
     */
111 4
    protected function getDsn()
112
    {
113 4
        $driver = $this->getDsnDriver();
114 4
        $host = $this->getHost();
115 4
        $database = $this->getDatabase();
116 4
        $dsn = $driver . ":host=" . $host;
117 4
        if (!is_null($database)) {
118 1
            $dsn .= ";dbname=" . $database;
119
        }
120 4
        return $dsn;
121
    }
122
123
    /**
124
     * @return int
125
     */
126 3
    public function getFetchMode()
127
    {
128 3
        return $this->fetchMode;
129
    }
130
131
    /**
132
     * @param int $fetchMode
133
     * @return $this
134
     */
135 1
    public function setFetchMode($fetchMode = \PDO::FETCH_ASSOC)
136
    {
137 1
        if ($fetchMode) {
138 1
            $this->fetchMode = (int)$fetchMode;
139 1
            $this->log(Logger::DEBUG, 'Fetch Mode changed', array('fetchMode' => $fetchMode));
140
        }
141 1
        return $this;
142
    }
143
144
    /**
145
     * @param string $sql
146
     * @return bool
147
     * @throws Exception
148
     */
149 2
    public function rawExecute($sql)
150
    {
151
        try {
152 2
            $this->log(Logger::DEBUG, 'Raw execute query', array('sql' => $sql));
153 2
            return $this->getDriver()->prepare($sql)->execute();
154 1
        } catch (\PDOException $e) {
155 1
            $this->log(Logger::ERROR, $e->getMessage(), array('error' => $e));
156 1
            throw new Exception($e->getMessage(), (int)$e->getCode(), $e);
157
        }
158
    }
159
160
    /**
161
     * Fetch a row for a given statement
162
     * @param object $statement
163
     * @param int $cursorOrientation Cursor orientation (next by default)
164
     * @param int $cursorOffset Cursor offset (0 by default)
165
     * @return array
166
     * @throws Exception
167
     */
168 3
    public function fetchRow($statement, $cursorOrientation = self::CURSOR_NEXT, $cursorOffset = 0)
169
    {
170 3
        if (!$statement instanceof \PDOStatement) {
171 1
            $this->log(Logger::ERROR, 'Statement not in right format', array('statement' => $statement));
172 1
            throw new Exception('Statement not in right format');
173
        }
174
175
        try {
176 2
            return $statement->fetch($this->getFetchMode(), (int)$cursorOrientation, (int)$cursorOffset);
177 1
        } catch (\PDOException $e) {
178 1
            $this->log(Logger::ERROR, $e->getMessage(), array('error' => $e));
179 1
            throw new Exception($e->getMessage(), (int)$e->getCode(), $e);
180
        }
181
    }
182
183
    /**
184
     * @return \PDO
185
     * @throws Exception
186
     */
187 7
    public function getDriver()
188
    {
189 7
        if (is_null($this->instance)) {
190
            try {
191 7
                $this->instance = $this->getPdo(
192 7
                    $this->getDsn(),
193 7
                    $this->getLogin(),
194 7
                    $this->getPassword(),
195 7
                    $this->getOptions()
196
                );
197 1
            } catch (\Exception $e) {
198 1
                $this->log(Logger::CRITICAL, 'Unable to connect database', (array)$this->getSettings());
199 1
                throw new Exception('Unable to connect database', $e->getCode(), $e);
200
            }
201 6
            $this->defaultConfiguration($this->instance);
202
        }
203 6
        return $this->instance;
204
    }
205
206
    /**
207
     * @param \Pdo $instance
208
     * @return $this
209
     */
210
    abstract protected function defaultConfiguration(\Pdo $instance);
211
212
    /**
213
     * @param string $dsn
214
     * @param string $login
215
     * @param string $password
216
     * @param array $options
217
     * @return \PDO
218
     */
219 1
    protected function getPdo($dsn, $login = null, $password = null, array $options = null)
220
    {
221 1
        return new \PDO($dsn, $login, $password, $options);
222
    }
223
224
    /**
225
     * Force reconnection
226
     * @return $this
227
     */
228 1
    public function forceReconnect()
229
    {
230 1
        $this->instance = null;
231 1
        return $this;
232
    }
233
}
234