Completed
Push — master ( 8fa5d0...315cf2 )
by Beniamin
06:27
created

AbstractBuilder::setParameter()   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 0
Metric Value
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 2
crap 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\QueryBuilder;
13
14
use Phuria\UnderQuery\Parameter\ParameterCollection;
15
use Phuria\UnderQuery\Parameter\ParameterCollectionInterface;
16
use Phuria\UnderQuery\Query\Query;
17
use Phuria\UnderQuery\Reference\ReferenceCollection;
18
use Phuria\UnderQuery\Reference\ReferenceCollectionInterface;
19
use Phuria\UnderQuery\Statement\StatementInterface;
20
use Phuria\UnderQuery\Table\AbstractTable;
21
22
/**
23
 * @author Beniamin Jonatan Šimko <[email protected]>
24
 */
25
abstract class AbstractBuilder implements BuilderInterface
26
{
27
    /**
28
     * @var QueryBuilderFacade
29
     */
30
    private $facade;
31
32
    /**
33
     * @var ReferenceCollectionInterface
34
     */
35
    private $referenceCollection;
36
37
    /**
38
     * @var ParameterCollectionInterface
39
     */
40
    private $parameterCollection;
41
42
    /**
43
     * @var AbstractTable[] $tables
44
     */
45
    private $rootTables = [];
46
47
    /**
48
     * @param QueryBuilderFacade $facade
49
     */
50 8
    public function __construct(QueryBuilderFacade $facade)
51
    {
52 8
        $this->facade = $facade;
53 8
        $this->parameterCollection = new ParameterCollection();
54 8
        $this->referenceCollection = new ReferenceCollection();
55 8
    }
56
57
    /**
58
     * @return BuilderInterface
59
     */
60 1
    public function getQueryBuilder()
61
    {
62 1
        return $this;
63
    }
64
65
    /**
66
     * @return ParameterCollectionInterface
67
     */
68 3
    public function getParameters()
69
    {
70 3
        return $this->parameterCollection;
71
    }
72
73
    /**
74
     * @return ReferenceCollectionInterface
75
     */
76 5
    public function getReferences()
77
    {
78 5
        return $this->referenceCollection;
79
    }
80
81
    /**
82
     * @param mixed  $table
83
     * @param string $alias
0 ignored issues
show
Documentation introduced by
Should the type for parameter $alias not be string|null?

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...
84
     *
85
     * @return AbstractTable
86
     */
87 1
    public function addRootTable($table, $alias = null)
88
    {
89 1
        $this->rootTables[] = $table = $this->createTable($table, $alias);
90
91 1
        return $table;
92
    }
93
94
    /**
95
     * @return AbstractTable[]
96
     */
97 4
    public function getRootTables()
98
    {
99 4
        return $this->rootTables;
100
    }
101
102
    /**
103
     * @param mixed       $table
104
     * @param string|null $alias
105
     *
106
     * @return AbstractTable
107
     */
108 1
    public function createTable($table, $alias = null)
109
    {
110 1
        $table = $this->facade->createTable($this, $table);
111
112 1
        if ($alias) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $alias of type string|null is loosely compared to true; this is ambiguous if the string can be empty. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
113 1
            $table->setAlias($alias);
114 1
        }
115
116 1
        return $table;
117
    }
118
119
    /**
120
     * @inheritdoc
121
     */
122 1
    public function objectToString($object)
123
    {
124 1
        return $this->getReferences()->createReference($object);
125
    }
126
127
    /**
128
     * @inheritdoc
129
     */
130 1
    public function setParameter($name, $value)
131
    {
132 1
        $parameter = $this->getParameters()->getParameter($name);
133 1
        $parameter->setValue($value);
134
135 1
        return $this;
136
    }
137
138
    /**
139
     * @return Query
140
     */
141 1
    public function buildQuery()
142
    {
143 1
        return new Query($this->buildSQL(), $this->parameterCollection->toArray());
144
    }
145
146
    /**
147
     * @inheritdoc
148
     */
149 3
    public function buildSQL()
150
    {
151 3
        return $this->facade->buildSQL($this);
152
    }
153
154
    /**
155
     * @return StatementInterface
156
     */
157 1
    public function buildStatement()
158
    {
159 1
        return $this->facade->buildStatement($this->buildSQL(), $this->getParameters()->toArray());
160
    }
161
}