Completed
Push — master ( 986a44...0cd77c )
by Beniamin
04:00
created

Query   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Test Coverage

Coverage 50%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 91
ccs 12
cts 24
cp 0.5
rs 10
c 0
b 0
f 0

7 Methods

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