Completed
Push — master ( b3ba9b...5f072b )
by Sébastien
18:57 queued 16:38
created

GenericPdo::quoteValue()   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 1
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 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
45 9
        } catch (Exception\InvalidArgumentException $e) {
46 5
            throw $e;
47 2
        } catch (\Exception $e) {
48 2
            $eclass = get_class($e);
49 2
            $msg = "GenericPdo '$eclass' : {$e->getMessage()} [$query]";
50 2
            throw new Exception\InvalidArgumentException($msg);
51
        }
52 9
        return $results;
53
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 3
    public function quoteValue($value)
60
    {
61 3
        return $this->resource->quote($value);
62
    }
63
64
65
    /**
66
     * Check extension
67
     * @throws Exception\RuntimeException
68
     * @return void
69
     */
70 19
    protected function checkEnvironment()
71
    {
72 19
        if (!extension_loaded('PDO')) {
73
            throw new Exception\RuntimeException('The PDO extension is not loaded');
74
        }
75 19
    }
76
}
77