Completed
Push — master ( fd0133...375486 )
by Beniamin
02:35
created

Query::getConnection()   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 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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\Query;
13
14
use Phuria\SQLBuilder\Connection\ConnectionInterface;
15
use Phuria\SQLBuilder\Parameter\ParameterManagerInterface;
16
use Phuria\SQLBuilder\Statement\StatementInterface;
17
18
/**
19
 * @author Beniamin Jonatan Šimko <[email protected]>
20
 */
21
class Query
22
{
23
    /**
24
     * @var string $sql
25
     */
26
    private $sql;
27
28
    /**
29
     * @var ParameterManagerInterface $parameterManager
30
     */
31
    private $parameterManager;
32
33
    /**
34
     * @var ConnectionInterface $connection
35
     */
36
    private $connection;
37
38
    /**
39
     * @param string                    $sql
40
     * @param ParameterManagerInterface $parameterManager
41
     * @param ConnectionInterface       $connection
42
     */
43 2
    public function __construct(
44
        $sql,
45
        ParameterManagerInterface $parameterManager,
46
        ConnectionInterface $connection
47
    ) {
48 2
        $this->sql = $sql;
49 2
        $this->parameterManager = $parameterManager;
50 2
        $this->connection = $connection;
51 2
    }
52
53
    /**
54
     * @return string
55
     */
56 1
    public function getSQL()
57
    {
58 1
        return $this->sql;
59
    }
60
61
    /**
62
     * @return ConnectionInterface
63
     */
64 1
    public function getConnection()
65
    {
66 1
        return $this->connection;
67
    }
68
69
    /**
70
     * @return StatementInterface
71
     */
72 1
    public function buildStatement()
73
    {
74 1
        $stmt = $this->connection->prepare($this->sql);
75 1
        $this->parameterManager->bindStatement($stmt);
76
77 1
        return $stmt;
78
    }
79
80
    /**
81
     * @return mixed
82
     */
83 1
    public function fetchScalar()
84
    {
85 1
        $stmt = $this->buildStatement();
86 1
        $stmt->execute();
87 1
        $result = $stmt->fetch(\PDO::FETCH_ASSOC);
88
89 1
        if (null === $result) {
90
            return null;
91
        }
92
93 1
        return reset($result);
94
    }
95
96
    /**
97
     * @param int|string $name
98
     * @param mixed      $value
99
     *
100
     * @return $this
101
     */
102
    public function setParameter($name, $value)
103
    {
104
        $this->parameterManager->createOrGetParameter($name)->setValue($value);
105
106
        return $this;
107
    }
108
}