Completed
Push — master ( ad3a38...ce24f7 )
by Beniamin
02:38
created

Query::setParameter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 2
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
0 ignored issues
show
Documentation introduced by
Should the type for parameter $connection not be null|ConnectionInterface?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
42
     */
43 16
    public function __construct(
44
        $sql,
45
        ParameterManagerInterface $parameterManager,
46
        ConnectionInterface $connection = null
47
    ) {
48 16
        $this->sql = $sql;
49 16
        $this->parameterManager = $parameterManager;
50 16
        $this->connection = $connection;
51 16
    }
52
53
    /**
54
     * @return string
55
     */
56 11
    public function getSQL()
57
    {
58 11
        return $this->sql;
59
    }
60
61
    /**
62
     * @return ConnectionInterface
63
     */
64 2
    public function getConnection()
65
    {
66 2
        return $this->connection;
67
    }
68
69
    /**
70
     * @return StatementInterface
71
     */
72 4
    public function buildStatement()
73
    {
74 4
        $stmt = $this->connection->prepare($this->sql);
75 4
        $this->parameterManager->bindStatement($stmt);
76
77 4
        return $stmt;
78
    }
79
80
    /**
81
     * @return mixed
82
     */
83 3
    public function fetchScalar()
84
    {
85 3
        $stmt = $this->buildStatement();
86 3
        $stmt->execute();
87
88 3
        return $stmt->fetchScalar();
89
    }
90
91
    /**
92
     * @param int|string $name
93
     * @param mixed      $value
94
     *
95
     * @return $this
96
     */
97 2
    public function setParameter($name, $value)
98
    {
99 2
        $this->parameterManager->createOrGetParameter($name)->setValue($value);
100
101 2
        return $this;
102
    }
103
}