Completed
Push — master ( eeabde...efa6a6 )
by Beniamin
02:56
created

AbstractTable   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 210
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 74.51%

Importance

Changes 10
Bugs 0 Features 7
Metric Value
wmc 20
c 10
b 0
f 7
lcom 1
cbo 3
dl 0
loc 210
ccs 38
cts 51
cp 0.7451
rs 10

17 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __toString() 0 4 1
getTableName() 0 1 ?
A compile() 0 20 4
A getAlias() 0 4 1
A setAlias() 0 6 1
A isJoin() 0 4 1
A getJoinType() 0 4 1
A setJoinType() 0 7 1
A isRoot() 0 4 1
A setRoot() 0 6 1
A getAliasOrName() 0 4 2
A addSelect() 0 6 1
A joinOn() 0 6 1
A getJoinOn() 0 4 1
A column() 0 4 1
A autoAlias() 0 6 1
1
<?php
2
3
namespace Phuria\QueryBuilder\Table;
4
5
use Phuria\QueryBuilder\QueryBuilder;
6
use Phuria\QueryBuilder\ReferenceManager;
7
8
/**
9
 * @author Beniamin Jonatan Šimko <[email protected]>
10
 */
11
abstract class AbstractTable
12
{
13
    const CROSS_JOIN = 'CROSS JOIN';
14
    const LEFT_JOIN = 'LEFT JOIN';
15
    const INNER_JOIN = 'INNER JOIN';
16
17
    /**
18
     * @var QueryBuilder $qb
19
     */
20
    private $qb;
21
22
    /**
23
     * @var string $tableAlias
24
     */
25
    private $tableAlias;
26
27
    /**
28
     * @var string $joinType
29
     */
30
    private $joinType;
31
32
    /**
33
     * @var string $joinOn
34
     */
35
    private $joinOn;
36
37
    /**
38
     * @var bool $root
39
     */
40
    private $root = false;
41
42
    /**
43
     * @var bool $join
44
     */
45
    private $join = false;
46
47
    /**
48
     * @param QueryBuilder $qb
49
     */
50 22
    public function __construct(QueryBuilder $qb)
51
    {
52 22
        $this->qb = $qb;
53 22
    }
54
55
    /**
56
     * @return ReferenceManager
57
     */
58 13
    public function __toString()
59
    {
60 13
        return $this->qb->getReferenceManager()->register($this);
61
    }
62
63
    /**
64
     * @return string
65
     */
66
    abstract public function getTableName();
67
68
    /**
69
     * @inheritdoc
70
     */
71
    public function compile()
72
    {
73
        $declaration = '';
74
75
        if ($this->isJoin()) {
76
            $declaration .= $this->getJoinType() . ' ';
77
        }
78
79
        $declaration .= $this->getTableName();
80
81
        if ($alias = $this->getAlias()) {
82
            $declaration .= ' AS ' . $alias;
83
        }
84
85
        if ($joinOn = $this->getJoinOn()) {
86
            $declaration .= ' ON ' . $joinOn;
87
        }
88
89
        return $declaration;
90
    }
91
92
    /**
93
     * @return string
94
     */
95 20
    public function getAlias()
96
    {
97 20
        return $this->tableAlias;
98
    }
99
100
    /**
101
     * @param string $alias
102
     *
103
     * @return $this
104
     */
105 8
    public function setAlias($alias)
106
    {
107 8
        $this->tableAlias = $alias;
108
109 8
        return $this;
110
    }
111
112
    /**
113
     * @return bool
114
     */
115 19
    public function isJoin()
116
    {
117 19
        return $this->join;
118
    }
119
120
    /**
121
     * @return string
122
     */
123 3
    public function getJoinType()
124
    {
125 3
        return $this->joinType;
126
    }
127
128
    /**
129
     * @param string $joinType
130
     *
131
     * @return $this
132
     */
133 3
    public function setJoinType($joinType)
134
    {
135 3
        $this->joinType = $joinType;
136 3
        $this->join = true;
137
138 3
        return $this;
139
    }
140
141
    /**
142
     * @return bool
143
     */
144 20
    public function isRoot()
145
    {
146 20
        return $this->root;
147
    }
148
149
    /**
150
     * @param bool $root
151
     *
152
     * @return $this
153
     */
154 22
    public function setRoot($root)
155
    {
156 22
        $this->root = $root;
157
158 22
        return $this;
159
    }
160
161
    /**
162
     * @return string
163
     */
164 13
    public function getAliasOrName()
165
    {
166 13
        return $this->getAlias() ?: $this->getTableName();
167
    }
168
169
    /**
170
     * @param string $clause
171
     *
172
     * @return $this
173
     */
174 5
    public function addSelect($clause)
175
    {
176 5
        $this->qb->addSelect($clause);
177
178 5
        return $this;
179
    }
180
181
    /**
182
     * @param string $clause
183
     *
184
     * @return $this
185
     */
186 2
    public function joinOn($clause)
187
    {
188 2
        $this->joinOn = $clause;
189
190 2
        return $this;
191
    }
192
193
    /**
194
     * @return string
195
     */
196 3
    public function getJoinOn()
197
    {
198 3
        return $this->joinOn;
199
    }
200
201
    /**
202
     * @param string $name
203
     *
204
     * @return string
205
     */
206 13
    public function column($name)
207
    {
208 13
        return $this . '.' . $name;
209
    }
210
211
    /**
212
     * @return $this
213
     */
214 1
    public function autoAlias()
215
    {
216 1
        $this->tableAlias = $this->qb->getAliasManager()->generateNextTableAlias();
217
218 1
        return $this;
219
    }
220
}