Completed
Push — master ( 349bb5...81542e )
by Alessandro
02:39
created

QueryBuilder::and()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 2
cts 2
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
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 16
    public function __construct(Collection $collection)
27
    {
28 16
        $this->collection = $collection;
29 16
        $this->queryType = Query::TYPE_FIND;
30 16
        $this->expression = new Expression();
31 16
        $this->options = [];
32 16
    }
33
34
    /**
35
     * @return $this
36
     */
37 6
    public function find()
38
    {
39 6
        return $this->setType(Query::TYPE_FIND);
40
    }
41
42
    /**
43
     * @return $this
44
     */
45 1
    public function count()
46
    {
47 1
        return $this->setType(Query::TYPE_COUNT);
48
    }
49
50
    /**
51
     * @param int $type
52
     *
53
     * @return $this
54
     */
55 7
    protected function setType(int $type)
56
    {
57 7
        $this->queryType = $type;
58
59 7
        return $this;
60
    }
61
62
    /**
63
     * @param array|Expression $expression
64
     *
65
     * @return $this
66
     */
67
    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...
68
    {
69 6
        $this->expression->and(...func_get_args());
70
71 6
        return $this;
72
    }
73
74
    /**
75
     * @param array|Expression $expression
76
     *
77
     * @return $this
78
     */
79 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...
80
    {
81 2
        $this->expression->or(...func_get_args());
82
83 2
        return $this;
84
    }
85
86
    /**
87
     * @param string $field
88
     * @param mixed  $value
89
     *
90
     * @return $this
91
     */
92 1
    public function equal(string $field, $value)
93
    {
94 1
        $this->expression->equal($field, $value);
95
96 1
        return $this;
97
    }
98
99
    /**
100
     * @param string $field
101
     * @param mixed  $value
102
     *
103
     * @return $this
104
     */
105 2
    public function notEqual(string $field, $value)
106
    {
107 2
        $this->expression->notEqual($field, $value);
108
109 2
        return $this;
110
    }
111
112
    /**
113
     * @return Query
114
     */
115 16
    public function getQuery(): Query
116
    {
117 16
        return new Query(
118 16
            $this->collection,
119 16
            $this->queryType,
120 16
            $this->expression->getExpressionFilters(),
121 16
            $this->options
122
        );
123
    }
124
125
    /**
126
     * @return int
127
     */
128 2
    public function getQueryType(): int
129
    {
130 2
        return $this->queryType;
131
    }
132
133
    /**
134
     * @param array $fields
135
     *
136
     * @return $this
137
     */
138 1
    public function sort(array $fields)
139
    {
140 1
        return $this->setQueryOption('sort', $fields);
141
    }
142
143
    /**
144
     * @param int $limit
145
     *
146
     * @return $this
147
     */
148 1
    public function limit(int $limit)
149
    {
150 1
        return $this->setQueryOption('limit', $limit);
151
    }
152
153
    /**
154
     * @param int $skip
155
     *
156
     * @return $this
157
     */
158 1
    public function skip(int $skip)
159
    {
160 1
        return $this->setQueryOption('skip', $skip);
161
    }
162
163
    /**
164
     * @param array $projection
165
     *
166
     * @return $this
167
     */
168 1
    public function select(...$projection)
169
    {
170 1
        $this->setQueryOption('projection', array_fill_keys($projection, 1));
171
172 1
        if (!in_array('_id', $projection)) {
173 1
            $this->options['projection']['_id'] = -1;
174
        }
175
176 1
        return $this;
177
    }
178
179
    /**
180
     * @param string $option
181
     * @param mixed  $value
182
     *
183
     * @return $this
184
     */
185 5
    public function setQueryOption(string $option, $value)
186
    {
187 5
        $this->options[$option] = $value;
188
189 5
        return $this;
190
    }
191
192
    /**
193
     * @return Expression
194
     */
195 3
    public function expr(): Expression
196
    {
197 3
        return new Expression();
198
    }
199
}