Passed
Push — master ( a4f7f2...d3cc39 )
by Melech
04:09
created

SqlSelectQueryBuilder   A

Complexity

Total Complexity 22

Size/Duplication

Total Lines 182
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 22
eloc 53
c 1
b 0
f 0
dl 0
loc 182
rs 10

14 Methods

Rating   Name   Duplication   Size   Complexity  
A getOrderByQuery() 0 5 3
A getGroupByQuery() 0 5 3
A getOffsetQuery() 0 5 3
A getLimitQuery() 0 5 3
A isCount() 0 3 1
A __toString() 0 12 1
A withAddedGroupBy() 0 7 1
A withAddedColumns() 0 7 1
A withColumns() 0 7 1
A withGroupBy() 0 7 1
A withOffset() 0 7 1
A withOrderBy() 0 7 1
A withAddedOrderBy() 0 7 1
A withLimit() 0 7 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Valkyrja Framework package.
7
 *
8
 * (c) Melech Mizrachi <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Valkyrja\Orm\QueryBuilder;
15
16
use Valkyrja\Orm\Constant\Statement;
0 ignored issues
show
Bug introduced by
The type Valkyrja\Orm\Constant\Statement was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Valkyrja\Orm\Data\OrderBy;
18
use Valkyrja\Orm\QueryBuilder\Contract\SelectQueryBuilder as Contract;
19
20
/**
21
 * Class SqlSelectQueryBuilder.
22
 *
23
 * @author Melech Mizrachi
24
 */
25
class SqlSelectQueryBuilder extends SqlQueryBuilder implements Contract
26
{
27
    /** @var non-empty-string[] */
0 ignored issues
show
Documentation Bug introduced by
The doc comment non-empty-string[] at position 0 could not be parsed: Unknown type name 'non-empty-string' at position 0 in non-empty-string[].
Loading history...
28
    protected array $columns = ['*'];
29
    /** @var string[] */
30
    protected array $groupBy = [];
31
    /** @var OrderBy[] */
32
    protected array $orderBy = [];
33
    /** @var int|null */
34
    protected int|null $limit = null;
35
    /** @var int|null */
36
    protected int|null $offset = null;
37
38
    /**
39
     * @inheritDoc
40
     */
41
    public function withColumns(string ...$columns): static
42
    {
43
        $new = clone $this;
44
45
        $new->columns = $columns;
46
47
        return $new;
48
    }
49
50
    /**
51
     * @inheritDoc
52
     */
53
    public function withAddedColumns(string ...$columns): static
54
    {
55
        $new = clone $this;
56
57
        $new->columns = array_merge($new->columns, $columns);
58
59
        return $new;
60
    }
61
62
    /**
63
     * @inheritDoc
64
     */
65
    public function withGroupBy(string ...$groupBy): static
66
    {
67
        $new = clone $this;
68
69
        $new->groupBy = $groupBy;
70
71
        return $new;
72
    }
73
74
    /**
75
     * @inheritDoc
76
     */
77
    public function withAddedGroupBy(string ...$groupBy): static
78
    {
79
        $new = clone $this;
80
81
        $new->groupBy = array_merge($new->groupBy, $groupBy);
82
83
        return $new;
84
    }
85
86
    /**
87
     * @inheritDoc
88
     */
89
    public function withOrderBy(OrderBy ...$orderBy): static
90
    {
91
        $new = clone $this;
92
93
        $new->orderBy = $orderBy;
94
95
        return $new;
96
    }
97
98
    /**
99
     * @inheritDoc
100
     */
101
    public function withAddedOrderBy(OrderBy ...$orderBy): static
102
    {
103
        $new = clone $this;
104
105
        $new->orderBy = array_merge($new->orderBy, $orderBy);
106
107
        return $new;
108
    }
109
110
    /**
111
     * @inheritDoc
112
     */
113
    public function withLimit(int $limit): static
114
    {
115
        $new = clone $this;
116
117
        $new->limit = $limit;
118
119
        return $new;
120
    }
121
122
    /**
123
     * @inheritDoc
124
     */
125
    public function withOffset(int $offset): static
126
    {
127
        $new = clone $this;
128
129
        $new->offset = $offset;
130
131
        return $new;
132
    }
133
134
    /**
135
     * @inheritDoc
136
     */
137
    public function __toString(): string
138
    {
139
        return Statement::SELECT
140
            . ' ' . implode(', ', $this->columns)
141
            . ' ' . Statement::FROM
142
            . " $this->from"
143
            . $this->getJoinQuery()
144
            . $this->getWhereQuery()
145
            . $this->getGroupByQuery()
146
            . $this->getOrderByQuery()
147
            . $this->getLimitQuery()
148
            . $this->getOffsetQuery();
149
    }
150
151
    /**
152
     * Get the GROUP BY part of a query statement.
153
     *
154
     * @return string
155
     */
156
    protected function getGroupByQuery(): string
157
    {
158
        return empty($this->orderBy) || $this->isCount()
159
            ? ''
160
            : ' ' . Statement::GROUP_BY . ' ' . implode(', ', $this->groupBy);
161
    }
162
163
    /**
164
     * Get the ORDER BY part of a query statement.
165
     *
166
     * @return string
167
     */
168
    protected function getOrderByQuery(): string
169
    {
170
        return empty($this->orderBy) || $this->isCount()
171
            ? ''
172
            : ' ' . Statement::ORDER_BY . ' ' . implode(', ', $this->orderBy);
173
    }
174
175
    /**
176
     * Get the LIMIT part of a query statement.
177
     *
178
     * @return string
179
     */
180
    protected function getLimitQuery(): string
181
    {
182
        return $this->limit === null || $this->isCount()
183
            ? ''
184
            : ' ' . Statement::LIMIT . ' ' . ((string) $this->limit);
185
    }
186
187
    /**
188
     * Get the OFFSET part of a query statement.
189
     *
190
     * @return string
191
     */
192
    protected function getOffsetQuery(): string
193
    {
194
        return $this->offset === null || $this->isCount()
195
            ? ''
196
            : ' ' . Statement::OFFSET . ' ' . ((string) $this->offset);
197
    }
198
199
    /**
200
     * Determine whether this is a count statement.
201
     *
202
     * @return bool
203
     */
204
    protected function isCount(): bool
205
    {
206
        return str_starts_with($this->columns[0], 'COUNT');
207
    }
208
}
209