Query::getConnection()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * This file is part of UnderQuery 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\UnderQuery\Query;
13
14
use Doctrine\DBAL\Driver\Connection as ConnectionInterface;
15
use Phuria\UnderQuery\Parameter\ParameterCollection;
16
use Phuria\UnderQuery\Parameter\ParameterCollectionInterface;
17
18
/**
19
 * @author Beniamin Jonatan Šimko <[email protected]>
20
 */
21
class Query implements QueryInterface
22
{
23
    /**
24
     * @var string
25
     */
26
    private $compiledSQL;
27
28
    /**
29
     * @var ParameterCollectionInterface
30
     */
31
    private $parameterCollection;
32
33
    /**
34
     * @var ConnectionInterface|null
35
     */
36
    private $connection;
37
38
    /**
39
     * @param string                   $compiledSQL
40
     * @param array                    $parameters
41
     * @param ConnectionInterface|null $connection
42
     */
43 3
    public function __construct($compiledSQL, array $parameters = [], ConnectionInterface $connection = null)
44
    {
45 3
        $this->compiledSQL = $compiledSQL;
46 3
        $this->parameterCollection = new ParameterCollection($parameters);
47 3
        $this->connection = $connection;
48 3
    }
49
50
    /**
51
     * @return string
52
     */
53 1
    public function getSQL()
54
    {
55 1
        return $this->compiledSQL;
56
    }
57
58
    /**
59
     * @return ParameterCollectionInterface
60
     */
61 2
    public function getParameters()
62
    {
63 2
        return $this->parameterCollection;
64
    }
65
66
    /**
67
     * @return ConnectionInterface|null
68
     */
69
    public function getConnection()
70
    {
71
        return $this->connection;
72
    }
73
74
    /**
75
     * @param int|string $name
76
     * @param mixed      $value
77
     *
78
     * @return $this
79
     */
80 1
    public function setParameter($name, $value)
81
    {
82 1
        $this->getParameters()->setValue($name, $value);
83
84 1
        return $this;
85
    }
86
87
    /**
88
     * @inheritdoc
89
     */
90
    public function prepare()
91
    {
92
        $stmt = $this->connection->prepare($this->getSQL());
93
94
        foreach ($this->getParameters()->toArray() as $parameter) {
95
            $stmt->bindValue($parameter->getName(), $parameter->getValue());
96
        }
97
98
        return $stmt;
99
    }
100
101
    /**
102
     * @inheritdoc
103
     */
104
    public function execute()
105
    {
106
        $stmt = $this->prepare();
107
        $stmt->execute();
108
109
        return $stmt;
110
    }
111
}