Completed
Push — master ( e25a2e...09c2a6 )
by Beniamin
03:10
created

Query::fetchScalar()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Phuria\QueryBuilder;
4
5
use Phuria\QueryBuilder\Connection\ConnectionInterface;
6
use Phuria\QueryBuilder\Parameter\ParameterManagerInterface;
7
use Phuria\QueryBuilder\Statement\StatementInterface;
8
9
/**
10
 * @author Beniamin Jonatan Šimko <[email protected]>
11
 */
12
class Query
13
{
14
    /**
15
     * @var string $sql
16
     */
17
    private $sql;
18
19
    /**
20
     * @var ParameterManagerInterface $parameterManager
21
     */
22
    private $parameterManager;
23
24
    /**
25
     * @var ConnectionInterface $connection
26
     */
27
    private $connection;
28
29
    /**
30
     * @param string                    $sql
31
     * @param ParameterManagerInterface $parameterManager
32
     * @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...
33
     */
34 12
    public function __construct(
35
        $sql,
36
        ParameterManagerInterface $parameterManager,
37
        ConnectionInterface $connection = null
38
    ) {
39 12
        $this->sql = $sql;
40 12
        $this->parameterManager = $parameterManager;
41 12
        $this->connection = $connection;
42 12
    }
43
44
    /**
45
     * @return string
46
     */
47 10
    public function getSQL()
48
    {
49 10
        return $this->sql;
50
    }
51
52
    /**
53
     * @return StatementInterface
54
     */
55 2
    public function buildStatement()
56
    {
57 2
        $stmt = $this->connection->prepare($this->sql);
58 2
        $this->parameterManager->bindStatement($stmt);
59
60 2
        return $stmt;
61
    }
62
63
    /**
64
     * @return mixed
65
     */
66 2
    public function fetchScalar()
67
    {
68 2
        $stmt = $this->buildStatement();
69 2
        $stmt->execute();
70
71 2
        return $stmt->fetchScalar();
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->parameterManager->createOrGetParameter($name)->setValue($value);
83
84 1
        return $this;
85
    }
86
}