Completed
Push — master ( dd2173...b87398 )
by Sébastien
02:57
created

GenericPdo::query()   C

Complexity

Conditions 7
Paths 29

Size

Total Lines 35
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 7

Importance

Changes 4
Bugs 0 Features 1
Metric Value
c 4
b 0
f 1
dl 0
loc 35
ccs 23
cts 23
cp 1
rs 6.7273
cc 7
eloc 21
nc 29
nop 1
crap 7
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 9
    public function query($query)
22
    {
23
        try {
24
            //$query = "select * from product";
25 9
            $stmt = $this->resource->prepare($query);
26
27 9
            if ($stmt === false) {
28 1
                $error = $this->resource->errorInfo();
29 1
                throw new Exception\InvalidArgumentException($error[2]);
30
            }
31
32 9
            if (!$stmt->execute()) {
33 4
                throw new Exception\InvalidArgumentException(
34 4
                    'Statement could not be executed (' . implode(' - ', $this->resource->errorInfo()) . ')'
35 4
                );
36
            };
37
38 9
            $results = new Resultset();
39 9
            if ($stmt->columnCount() > 0) {
40 5
                while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
41 5
                    $results->append($row);
42 5
                }
43 5
            }
44 9
            $stmt->closeCursor();
45
46 9
        } catch (Exception\InvalidArgumentException $e) {
47 5
            throw $e;
48 2
        } catch (\Exception $e) {
49 2
            $eclass = get_class($e);
50 2
            $msg = "GenericPdo '$eclass' : {$e->getMessage()} [$query]";
51 2
            throw new Exception\InvalidArgumentException($msg);
52
        }
53 9
        return $results;
54
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60 3
    public function quoteValue($value)
61
    {
62 3
        return $this->resource->quote($value);
63
    }
64
65
66
    /**
67
     * Check extension
68
     * @throws Exception\RuntimeException
69
     * @return void
70
     */
71 19
    protected function checkEnvironment()
72
    {
73 19
        if (!extension_loaded('PDO')) {
74
            throw new Exception\RuntimeException('The PDO extension is not loaded');
75
        }
76 19
    }
77
}
78