QueryBuilder::select()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
crap 2
1
<?php declare(strict_types = 1);
2
3
namespace Algatux\MongoDB\QueryBuilder;
4
5
use MongoDB\Collection;
6
7
/**
8
 * Class QueryBuilder.
9
 */
10
class QueryBuilder
11
{
12
    /** @var Collection */
13
    private $collection;
14
    /** @var Expression  */
15
    private $expression;
16
    /** @var array */
17
    private $options;
18
    /** @var int */
19
    private $queryType;
20
21
    /**
22
     * QueryBuilder constructor.
23
     *
24
     * @param Collection $collection
25
     */
26 18
    public function __construct(Collection $collection)
27
    {
28 18
        $this->collection = $collection;
29 18
        $this->queryType = Query::TYPE_FIND;
30 18
        $this->expression = new Expression();
31 18
        $this->options = [];
32 18
    }
33
34
    /**
35
     * Tells the query builder to execute a find
36
     *
37
     * @return $this
38
     */
39 8
    public function find()
40
    {
41 8
        return $this->setType(Query::TYPE_FIND);
42
    }
43
44
    /**
45
     * Tells the query builder to execute a count
46
     *
47
     * @return $this
48
     */
49 1
    public function count()
50
    {
51 1
        return $this->setType(Query::TYPE_COUNT);
52
    }
53
54
    /**
55
     * @param int $type
56
     *
57
     * @return $this
58
     */
59 9
    protected function setType(int $type)
60
    {
61 9
        $this->queryType = $type;
62
63 9
        return $this;
64
    }
65
66
    /**
67
     * Adds and filter
68
     *
69
     * @param array|Expression $expression
70
     *
71
     * @return $this
72
     */
73
    public function and($expression)
0 ignored issues
show
Unused Code introduced by
The parameter $expression is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
74
    {
75 6
        $this->expression->and(...func_get_args());
76
77 6
        return $this;
78
    }
79
80
    /**
81
     * Adds or filter
82
     *
83
     * @param array|Expression $expression
84
     *
85
     * @return $this
86
     */
87 2
    public function or($expression)
0 ignored issues
show
Unused Code introduced by
The parameter $expression is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
88
    {
89 2
        $this->expression->or(...func_get_args());
90
91 2
        return $this;
92
    }
93
94
    /**
95
     * Adds eq filter
96
     *
97
     * @param string $field
98
     * @param mixed  $value
99
     *
100
     * @return $this
101
     */
102 1
    public function equal(string $field, $value)
103
    {
104 1
        $this->expression->equal($field, $value);
105
106 1
        return $this;
107
    }
108
109
    /**
110
     * Adds ne filter
111
     *
112
     * @param string $field
113
     * @param mixed  $value
114
     *
115
     * @return $this
116
     */
117 2
    public function notEqual(string $field, $value)
118
    {
119 2
        $this->expression->notEqual($field, $value);
120
121 2
        return $this;
122
    }
123
124
    /**
125
     * Adds in filter
126
     *
127
     * @param string $field
128
     * @param array  $values
129
     *
130
     * @return $this
131
     */
132 1
    public function in(string $field, array $values)
133
    {
134 1
        $this->expression->in($field, $values);
135
136 1
        return $this;
137
    }
138
139
    /**
140
     * Adds nin filter
141
     *
142
     * @param string $field
143
     * @param array  $values
144
     *
145
     * @return $this
146
     */
147 1
    public function notIn(string $field, array $values)
148
    {
149 1
        $this->expression->notIn($field, $values);
150
151 1
        return $this;
152
    }
153
154
    /**
155
     * Returns a new Query set up with the builder configuration
156
     *
157
     * @return Query
158
     */
159 18
    public function getQuery(): Query
160
    {
161 18
        return new Query(
162 18
            $this->collection,
163 18
            $this->queryType,
164 18
            $this->expression->getExpressionFilters(),
165 18
            $this->options
166
        );
167
    }
168
169
    /**
170
     * Returns the query type
171
     *
172
     * @return int
173
     */
174 2
    public function getQueryType(): int
175
    {
176 2
        return $this->queryType;
177
    }
178
179
    /**
180
     * Sets the sort option
181
     *
182
     * @param array $fields
183
     *
184
     * @return $this
185
     */
186 2
    public function sort(array $fields)
187
    {
188 2
        return $this->setQueryOption('sort', $fields);
0 ignored issues
show
Deprecated Code introduced by
The method Algatux\MongoDB\QueryBui...ilder::setQueryOption() has been deprecated with message: use the right method if supported

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
189
    }
190
191
    /**
192
     * Sets the limit option
193
     *
194
     * @param int $limit
195
     *
196
     * @return $this
197
     */
198 1
    public function limit(int $limit)
199
    {
200 1
        return $this->setQueryOption('limit', $limit);
0 ignored issues
show
Deprecated Code introduced by
The method Algatux\MongoDB\QueryBui...ilder::setQueryOption() has been deprecated with message: use the right method if supported

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
201
    }
202
203
    /**
204
     * Sets the skip(offset) option
205
     *
206
     * @param int $skip
207
     *
208
     * @return $this
209
     */
210 1
    public function skip(int $skip)
211
    {
212 1
        return $this->setQueryOption('skip', $skip);
0 ignored issues
show
Deprecated Code introduced by
The method Algatux\MongoDB\QueryBui...ilder::setQueryOption() has been deprecated with message: use the right method if supported

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
213
    }
214
215
    /**
216
     * Sets the projection option
217
     *
218
     * @param array $projection
219
     *
220
     * @return $this
221
     */
222 1
    public function select(...$projection)
223
    {
224 1
        $this->setQueryOption('projection', array_fill_keys($projection, 1));
0 ignored issues
show
Deprecated Code introduced by
The method Algatux\MongoDB\QueryBui...ilder::setQueryOption() has been deprecated with message: use the right method if supported

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
225
226 1
        if (!in_array('_id', $projection)) {
227 1
            $this->options['projection']['_id'] = -1;
228
        }
229
230 1
        return $this;
231
    }
232
233
    /**
234
     * Sets the an actually unsupported option
235
     * @deprecated use the right method if supported
236
     *
237
     * @param string $option
238
     * @param mixed  $value
239
     *
240
     * @return $this
241
     */
242 6
    public function setQueryOption(string $option, $value)
243
    {
244 6
        $this->options[$option] = $value;
245
246 6
        return $this;
247
    }
248
249
    /**
250
     * Returns a new Expression
251
     *
252
     * @return Expression
253
     */
254 3
    public function expr(): Expression
255
    {
256 3
        return new Expression();
257
    }
258
}