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

GenericPdo   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 80%

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 55
wmc 8
lcom 1
cbo 3
ccs 16
cts 20
cp 0.8
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
B query() 0 19 5
A quoteValue() 0 4 1
A checkEnvironment() 0 6 2
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