Completed
Push — master ( dbcbc1...04d817 )
by Sébastien
02:01
created

GenericPdo::getResource()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Soluble\DbWrapper\Adapter\Pdo;
4
5
use Soluble\DbWrapper\Exception;
6
use Soluble\DbWrapper\Adapter\AdapterInterface;
7
use Soluble\DbWrapper\Result\Resultset;
8
9
abstract class GenericPdo implements AdapterInterface
10
{
11
    /**
12
     *
13
     * @var \PDO
14
     */
15
    protected $resource;    
16
17
18
    /**
19
     * {@inheritdoc}
20
     */
21 6
    public function query($query)
22
    {
23
        try {
24 6
            $stmt = $this->resource->query($query, \PDO::FETCH_ASSOC);
25 6
            if ($stmt === false) {
26 3
                throw new Exception\InvalidArgumentException("Query cannot be executed [$query].");
27
            }
28 6
            $results = new Resultset();
29 6
            foreach ($stmt as $row) {
30 3
                $results->append($row);
31 6
            }
32 6
        } catch (Exception\InvalidArgumentException $e) {
33 3
            throw $e;
34
        } catch (\Exception $e) {
35
            $msg = "PDOException : {$e->getMessage()} [$query]";
36
            throw new Exception\InvalidArgumentException($msg);
37
        }
38 6
        return $results;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44 2
    public function quoteValue($value)
45
    {
46 2
        return $this->resource->quote($value);
47
    }
48
49
50
    /**
51
     * Check extension
52
     * @throws Exception\RuntimeException
53
     * @return void
54
     */
55 12
    protected function checkEnvironment()
56
    {
57 12
        if (!extension_loaded('PDO')) {
58
            throw new Exception\RuntimeException('The PDO extension is not loaded');
59
        }
60 12
    }
61
        
62
    
63
}
64