JoinQuery   A
last analyzed

Complexity

Total Complexity 24

Size/Duplication

Total Lines 284
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 24
lcom 1
cbo 5
dl 0
loc 284
c 2
b 0
f 1
rs 10

20 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A setTable() 0 6 1
A leftJoin() 0 4 1
A join() 0 16 2
A addJoin() 0 16 2
A setJoin() 0 6 1
A rightJoin() 0 4 1
A crossJoin() 0 4 1
A innerJoin() 0 4 1
A on() 0 4 1
A joinCondition() 0 8 2
A isJoinSelect() 0 4 1
A isJoin() 0 4 1
A getJoinCondition() 0 4 1
A setJoinCondition() 0 6 1
A getJoinType() 0 4 1
A setJoinType() 0 6 1
A getJoins() 0 4 1
A setJoins() 0 6 1
A getAllJoins() 0 10 2
1
<?php
2
/**
3
 * Author: Nil Portugués Calderó <[email protected]>
4
 * Date: 12/25/14
5
 * Time: 11:41 AM.
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace NilPortugues\Sql\QueryBuilder\Manipulation;
12
13
use NilPortugues\Sql\QueryBuilder\Syntax\SyntaxFactory;
14
use NilPortugues\Sql\QueryBuilder\Syntax\Where;
15
16
/**
17
 * Class JoinQuery.
18
 */
19
class JoinQuery
20
{
21
    const JOIN_LEFT = 'LEFT';
22
    const JOIN_RIGHT = 'RIGHT';
23
    const JOIN_INNER = 'INNER';
24
    const JOIN_CROSS = 'CROSS';
25
26
    /**
27
     * @var Where
28
     */
29
    protected $joinCondition;
30
31
    /**
32
     * @var bool
33
     */
34
    protected $isJoin = false;
35
36
    /**
37
     * @var string
38
     */
39
    protected $joinType;
40
41
    /**
42
     * @var array
43
     */
44
    protected $joins = [];
45
46
    /**
47
     * @var Select
48
     */
49
    protected $select;
50
51
    /**
52
     * @param Select $select
53
     */
54
    public function __construct(Select $select)
55
    {
56
        $this->select = $select;
57
    }
58
59
    /**
60
     * @param string $table
61
     *
62
     * @return $this
63
     */
64
    public function setTable($table)
65
    {
66
        $this->select->setTable($table);
67
68
        return $this;
69
    }
70
71
    /**
72
     * @param string   $table
73
     * @param string   $selfColumn
74
     * @param string   $refColumn
75
     * @param string[] $columns
76
     *
77
     * @return Select
78
     */
79
    public function leftJoin($table, $selfColumn = null, $refColumn = null, $columns = [])
80
    {
81
        return $this->join($table, $selfColumn, $refColumn, $columns, self::JOIN_LEFT);
82
    }
83
84
    /**
85
     * @param string   $table
86
     * @param string   $selfColumn
87
     * @param string   $refColumn
88
     * @param string[] $columns
89
     * @param string   $joinType
90
     *
91
     * @return Select
92
     */
93
    public function join(
94
        $table,
95
        $selfColumn = null,
96
        $refColumn = null,
97
        $columns = [],
98
        $joinType = null
99
    ) {
100
        if (!isset($this->joins[$table])) {
101
            $select = QueryFactory::createSelect($table);
102
            $select->setColumns($columns);
103
            $select->setJoinType($joinType);
104
            $this->addJoin($select, $selfColumn, $refColumn);
105
        }
106
107
        return $this->joins[$table];
108
    }
109
110
    /**
111
     * @param Select $select
112
     * @param string $selfColumn
113
     * @param string $refColumn
114
     *
115
     * @return Select
116
     */
117
    public function addJoin(Select $select, $selfColumn, $refColumn)
118
    {
119
        $select->isJoin(true);
120
        $table = $select->getTable()->getName();
121
122
        if (!isset($this->joins[$table])) {
123
            $newColumn = array($selfColumn);
124
            $select->joinCondition()->equals(
125
                $refColumn,
126
                SyntaxFactory::createColumn($newColumn, $this->select->getTable())
127
            );
128
            $this->joins[$table] = $select;
129
        }
130
131
        return $this->joins[$table];
132
    }
133
134
    /**
135
     * Transforms Select in a joint.
136
     *
137
     * @param bool $isJoin
138
     *
139
     * @return $this
140
     */
141
    public function setJoin($isJoin = true)
142
    {
143
        $this->isJoin = $isJoin;
144
145
        return $this;
146
    }
147
148
    /**
149
     * @param string   $table
150
     * @param string   $selfColumn
151
     * @param string   $refColumn
152
     * @param string[] $columns
153
     *
154
     * @internal param null $selectClass
155
     *
156
     * @return Select
157
     */
158
    public function rightJoin($table, $selfColumn = null, $refColumn = null, $columns = [])
159
    {
160
        return $this->join($table, $selfColumn, $refColumn, $columns, self::JOIN_RIGHT);
161
    }
162
163
    /**
164
     * @param string   $table
165
     * @param string   $selfColumn
166
     * @param string   $refColumn
167
     * @param string[] $columns
168
     *
169
     * @return Select
170
     */
171
    public function crossJoin($table, $selfColumn = null, $refColumn = null, $columns = [])
172
    {
173
        return $this->join($table, $selfColumn, $refColumn, $columns, self::JOIN_CROSS);
174
    }
175
176
    /**
177
     * @param string   $table
178
     * @param string   $selfColumn
179
     * @param string   $refColumn
180
     * @param string[] $columns
181
     *
182
     * @return Select
183
     */
184
    public function innerJoin($table, $selfColumn = null, $refColumn = null, $columns = [])
185
    {
186
        return $this->join($table, $selfColumn, $refColumn, $columns, self::JOIN_INNER);
187
    }
188
189
    /**
190
     * Alias to joinCondition.
191
     *
192
     * @return Where
193
     */
194
    public function on()
195
    {
196
        return $this->joinCondition();
197
    }
198
199
    /**
200
     * WHERE constrains used for the ON clause of a (LEFT/RIGHT/INNER/CROSS) JOIN.
201
     *
202
     * @return Where
203
     */
204
    public function joinCondition()
205
    {
206
        if (!isset($this->joinCondition)) {
207
            $this->joinCondition = QueryFactory::createWhere($this->select);
208
        }
209
210
        return $this->joinCondition;
211
    }
212
213
    /**
214
     * @return bool
215
     */
216
    public function isJoinSelect()
217
    {
218
        return $this->isJoin;
219
    }
220
221
    /**
222
     * @return bool
223
     */
224
    public function isJoin()
225
    {
226
        return $this->isJoin;
227
    }
228
229
    /**
230
     * @return \NilPortugues\Sql\QueryBuilder\Syntax\Where
231
     */
232
    public function getJoinCondition()
233
    {
234
        return $this->joinCondition;
235
    }
236
237
    /**
238
     * @param \NilPortugues\Sql\QueryBuilder\Syntax\Where $joinCondition
239
     *
240
     * @return $this
241
     */
242
    public function setJoinCondition($joinCondition)
243
    {
244
        $this->joinCondition = $joinCondition;
245
246
        return $this;
247
    }
248
249
    /**
250
     * @return string
251
     */
252
    public function getJoinType()
253
    {
254
        return $this->joinType;
255
    }
256
257
    /**
258
     * @param string $joinType
259
     *
260
     * @return $this
261
     */
262
    public function setJoinType($joinType)
263
    {
264
        $this->joinType = $joinType;
265
266
        return $this;
267
    }
268
269
    /**
270
     * @return array
271
     */
272
    public function getJoins()
273
    {
274
        return $this->joins;
275
    }
276
277
    /**
278
     * @param array $joins
279
     *
280
     * @return $this
281
     */
282
    public function setJoins($joins)
283
    {
284
        $this->joins = $joins;
285
286
        return $this;
287
    }
288
289
    /**
290
     * @return array
291
     */
292
    public function getAllJoins()
293
    {
294
        $joins = $this->joins;
295
296
        foreach ($this->joins as $join) {
297
            $joins = \array_merge($joins, $join->getAllJoins());
298
        }
299
300
        return $joins;
301
    }
302
}
303