Completed
Push — master ( 58c9ab...b3ba9b )
by Sébastien
19:11 queued 01:31
created

GenericPdo::query()   C

Complexity

Conditions 7
Paths 23

Size

Total Lines 34
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 7.323

Importance

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