Completed
Push — master ( be00fa...152a7c )
by Beniamin
05:51 queued 02:08
created

PDOConnection::fetchScalar()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2.032

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
ccs 4
cts 5
cp 0.8
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
crap 2.032
1
<?php
2
3
/**
4
 * This file is part of Phuria SQL Builder package.
5
 *
6
 * Copyright (c) 2016 Beniamin Jonatan Šimko
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Phuria\SQLBuilder\Connection;
13
14
use Phuria\SQLBuilder\Parameter\QueryParameterInterface;
15
16
/**
17
 * @author Beniamin Jonatan Šimko <[email protected]>
18
 */
19
class PDOConnection implements ConnectionInterface
20
{
21
    /**
22
     * @var \PDO $wrappedConnection
23
     */
24
    private $wrappedConnection;
25
26
    /**
27
     * @param \PDO $connection
28
     */
29 2
    public function __construct(\PDO $connection)
30
    {
31 2
        $this->wrappedConnection = $connection;
32 2
    }
33
34
    /**
35
     * @param $SQL
36
     *
37
     * @return \PDOStatement
38
     */
39 2
    private function prepareStatement($SQL)
40
    {
41 2
        return $this->wrappedConnection->prepare($SQL);
42
    }
43
44
    /**
45
     * @param string                    $SQL
46
     * @param QueryParameterInterface[] $parameters
47
     *
48
     * @return \PDOStatement
49
     */
50 2
    private function getExecutedStatement($SQL, array $parameters = [])
51
    {
52 2
        $preparedStmt = $this->prepareStatement($SQL);
53
54 2
        foreach ($parameters as $parameter) {
55
            $preparedStmt->bindValue($parameter->getName(), $parameter->getValue());
56 2
        }
57
58 2
        $preparedStmt->execute();
59
60 2
        return $preparedStmt;
61
    }
62
63
    /**
64
     * @inheritdoc
65
     */
66 1
    public function fetchScalar($SQL, array $parameters = [])
67
    {
68 1
        $stmt = $this->getExecutedStatement($SQL, $parameters);
69
70 1
        if (0 < $stmt->rowCount()) {
71 1
            return $stmt->fetch(\PDO::FETCH_COLUMN);
72
        }
73
74
        return null;
75
    }
76
77
    /**
78
     * @inheritdoc
79
     */
80
    public function fetchRow($SQL, array $parameters = [])
81
    {
82
        $stmt = $this->getExecutedStatement($SQL, $parameters);
83
84
        if (0 < $stmt->rowCount()) {
85
            return $stmt->fetch(\PDO::FETCH_ASSOC);
86
        }
87
88
        return [];
89
    }
90
91
    /**
92
     * @inheritdoc
93
     */
94 1
    public function fetchAll($SQL, array $parameters = [])
95
    {
96 1
        $stmt = $this->getExecutedStatement($SQL, $parameters);
97
98 1
        if (0 < $stmt->rowCount()) {
99 1
            return $stmt->fetchAll(\PDO::FETCH_ASSOC);
100
        }
101
102
        return [];
103
    }
104
105
    /**
106
     * @inheritdoc
107
     */
108
    public function rowCount($SQL, array $parameters = [])
109
    {
110
        $stmt = $this->getExecutedStatement($SQL, $parameters);
111
112
        return $stmt->rowCount() ?: 0;
113
    }
114
}