Passed
Push — master ( ae1a26...486cfe )
by Petr
03:11
created

AConnector::offset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
namespace kalanis\kw_mapper\Search\Connector;
4
5
6
use kalanis\kw_mapper\Interfaces\IQueryBuilder;
7
use kalanis\kw_mapper\MapperException;
8
use kalanis\kw_mapper\Mappers\ForeignKey;
9
use kalanis\kw_mapper\Records\ARecord;
10
use kalanis\kw_mapper\Storage;
11
12
13
/**
14
 * Class AConnector
15
 * @package kalanis\kw_mapper\Search
16
 * Connect real sources into search engine
17
 */
18
abstract class AConnector
19
{
20
    use Database\TRecordsInJoins;
21
22
    /** @var ARecord */
23
    protected $basicRecord = null;
24
    /** @var Storage\Shared\QueryBuilder */
25
    protected $queryBuilder = null;
26
27
    /**
28
     * @param string $table
29
     * @param string $column
30
     * @param string $value
31
     * @throws MapperException
32
     * @return $this
33
     */
34 1
    public function notExact(string $table, string $column, $value): self
35
    {
36 1
        $aTable = $this->correctTable($table);
37 1
        $this->queryBuilder->addCondition(
38
            $aTable,
39 1
            $this->correctColumn($aTable, $column),
40
            IQueryBuilder::OPERATION_NEQ,
41
            $value
42
        );
43 1
        return $this;
44
    }
45
46
    /**
47
     * @param string $table
48
     * @param string $column
49
     * @param string $value
50
     * @throws MapperException
51
     * @return $this
52
     */
53 7
    public function exact(string $table, string $column, $value): self
54
    {
55 7
        $aTable = $this->correctTable($table);
56 7
        $this->queryBuilder->addCondition(
57
            $aTable,
58 7
            $this->correctColumn($aTable, $column),
59
            IQueryBuilder::OPERATION_EQ,
60
            $value
61
        );
62 6
        return $this;
63
    }
64
65
    /**
66
     * @param string $table
67
     * @param string $column
68
     * @param string $value
69
     * @param bool $equals
70
     * @throws MapperException
71
     * @return $this
72
     */
73 1
    public function from(string $table, string $column, $value, bool $equals = true): self
74
    {
75 1
        $aTable = $this->correctTable($table);
76 1
        $this->queryBuilder->addCondition(
77
            $aTable,
78 1
            $this->correctColumn($aTable, $column),
79 1
            $equals ? IQueryBuilder::OPERATION_GTE : IQueryBuilder::OPERATION_GT,
80
            $value
81
        );
82 1
        return $this;
83
    }
84
85
    /**
86
     * @param string $table
87
     * @param string $column
88
     * @param string $value
89
     * @param bool $equals
90
     * @throws MapperException
91
     * @return $this
92
     */
93 1
    public function to(string $table, string $column, $value, bool $equals = true): self
94
    {
95 1
        $aTable = $this->correctTable($table);
96 1
        $this->queryBuilder->addCondition(
97
            $aTable,
98 1
            $this->correctColumn($aTable, $column),
99 1
            $equals ? IQueryBuilder::OPERATION_LTE : IQueryBuilder::OPERATION_LT,
100
            $value
101
        );
102 1
        return $this;
103
    }
104
105
    /**
106
     * @param string $table
107
     * @param string $column
108
     * @param string $value
109
     * @throws MapperException
110
     * @return $this
111
     */
112 5
    public function like(string $table, string $column, $value): self
113
    {
114 5
        $aTable = $this->correctTable($table);
115 5
        $this->queryBuilder->addCondition(
116
            $aTable,
117 5
            $this->correctColumn($aTable, $column),
118
            IQueryBuilder::OPERATION_LIKE,
119
            $value
120
        );
121 4
        return $this;
122
    }
123
124
    /**
125
     * @param string $table
126
     * @param string $column
127
     * @param string $value
128
     * @throws MapperException
129
     * @return $this
130
     */
131 1
    public function notLike(string $table, string $column, $value): self
132
    {
133 1
        $aTable = $this->correctTable($table);
134 1
        $this->queryBuilder->addCondition(
135
            $aTable,
136 1
            $this->correctColumn($aTable, $column),
137
            IQueryBuilder::OPERATION_NLIKE,
138
            $value
139
        );
140 1
        return $this;
141
    }
142
143
    /**
144
     * @param string $table
145
     * @param string $column
146
     * @param string $pattern
147
     * @throws MapperException
148
     * @return $this
149
     */
150 1
    public function regexp(string $table, string $column, string $pattern): self
151
    {
152 1
        $aTable = $this->correctTable($table);
153 1
        $this->queryBuilder->addCondition(
154
            $aTable,
155 1
            $this->correctColumn($aTable, $column),
156
            IQueryBuilder::OPERATION_REXP,
157
            $pattern
158
        );
159 1
        return $this;
160
    }
161
162
    /**
163
     * @param string $table
164
     * @param string $column
165
     * @param string $min
166
     * @param string $max
167
     * @throws MapperException
168
     * @return $this
169
     */
170 1
    public function between(string $table, string $column, $min, $max): self
171
    {
172 1
        $aTable = $this->correctTable($table);
173 1
        $this->queryBuilder->addCondition($aTable, $this->correctColumn($aTable, $column), IQueryBuilder::OPERATION_GTE, $min);
174 1
        $this->queryBuilder->addCondition($aTable, $this->correctColumn($aTable, $column), IQueryBuilder::OPERATION_LTE, $max);
175 1
        return $this;
176
    }
177
178
    /**
179
     * @param string $table
180
     * @param string $column
181
     * @throws MapperException
182
     * @return $this
183
     */
184 1
    public function null(string $table, string $column): self
185
    {
186 1
        $aTable = $this->correctTable($table);
187 1
        $this->queryBuilder->addCondition(
188
            $aTable,
189 1
            $this->correctColumn($aTable, $column),
190
            IQueryBuilder::OPERATION_NULL
191
        );
192 1
        return $this;
193
    }
194
195
    /**
196
     * @param string $table
197
     * @param string $column
198
     * @throws MapperException
199
     * @return $this
200
     */
201 1
    public function notNull(string $table, string $column): self
202
    {
203 1
        $aTable = $this->correctTable($table);
204 1
        $this->queryBuilder->addCondition(
205
            $aTable,
206 1
            $this->correctColumn($aTable, $column),
207
            IQueryBuilder::OPERATION_NNULL
208
        );
209 1
        return $this;
210
    }
211
212
    /**
213
     * @param string $table
214
     * @param string $column
215
     * @param array<string|int|float> $values
216
     * @throws MapperException
217
     * @return $this
218
     */
219 1
    public function in(string $table, string $column, array $values): self
220
    {
221 1
        $aTable = $this->correctTable($table);
222 1
        $this->queryBuilder->addCondition(
223
            $aTable,
224 1
            $this->correctColumn($aTable, $column),
225
            IQueryBuilder::OPERATION_IN,
226
            $values
227
        );
228 1
        return $this;
229
    }
230
231
    /**
232
     * @param string $table
233
     * @param string $column
234
     * @param array<string|int|float> $values
235
     * @throws MapperException
236
     * @return $this
237
     */
238 1
    public function notIn(string $table, string $column, array $values): self
239
    {
240 1
        $aTable = $this->correctTable($table);
241 1
        $this->queryBuilder->addCondition(
242
            $aTable,
243 1
            $this->correctColumn($aTable, $column),
244
            IQueryBuilder::OPERATION_NIN,
245
            $values
246
        );
247 1
        return $this;
248
    }
249
250 1
    public function useAnd(): self
251
    {
252 1
        $this->queryBuilder->setRelations(IQueryBuilder::RELATION_AND);
253 1
        return $this;
254
    }
255
256 1
    public function useOr(): self
257
    {
258 1
        $this->queryBuilder->setRelations(IQueryBuilder::RELATION_OR);
259 1
        return $this;
260
    }
261
262 1
    public function limit(?int $limit): self
263
    {
264 1
        $this->queryBuilder->setLimit($limit);
265 1
        return $this;
266
    }
267
268 1
    public function offset(?int $offset): self
269
    {
270 1
        $this->queryBuilder->setOffset($offset);
271 1
        return $this;
272
    }
273
274
    /**
275
     * Add ordering by
276
     * @param string $table
277
     * @param string $column
278
     * @param string $direction
279
     * @throws MapperException
280
     * @return $this
281
     */
282 1
    public function orderBy(string $table, string $column, string $direction = IQueryBuilder::ORDER_ASC): self
283
    {
284 1
        $aTable = $this->correctTable($table);
285 1
        $this->queryBuilder->addOrderBy($aTable, $this->correctColumn($aTable, $column), $direction);
286 1
        return $this;
287
    }
288
289
    /**
290
     * Add grouping by
291
     * @param string $table
292
     * @param string $column
293
     * @throws MapperException
294
     * @return $this
295
     */
296 1
    public function groupBy(string $table, string $column): self
297
    {
298 1
        $aTable = $this->correctTable($table);
299 1
        $this->queryBuilder->addGroupBy($aTable, $this->correctColumn($aTable, $column));
300 1
        return $this;
301
    }
302
303
    /**
304
     * Add child which will be mounted to results
305
     * @param string $childAlias
306
     * @param string $joinType
307
     * @param string $parentAlias
308
     * @param string $customAlias
309
     * @throws MapperException
310
     * @return $this
311
     */
312 6
    public function child(string $childAlias, string $joinType = IQueryBuilder::JOIN_LEFT, string $parentAlias = '', string $customAlias = ''): self
313
    {
314
        // from mapper - children's mapper then there table name
315 6
        if (!empty($parentAlias)) {
316 3
            $parentLookup = $this->recordLookup($parentAlias);
317 3
            if ($parentLookup && $parentLookup->getRecord()) {
318 3
                $parentRecord = $parentLookup->getRecord();
319
            }
320
        } else {
321 5
            $parentRecord = $this->basicRecord;
322 5
            $parentAlias = $parentRecord->getMapper()->getAlias();
323
        }
324 6
        if (empty($parentRecord)) {
325 1
            throw new MapperException(sprintf('Unknown record for parent alias *%s*', $parentAlias));
326
        }
327
        /** @var array<string|int, ForeignKey> $parentKeys */
328 5
        $parentKeys = $parentRecord->getMapper()->getForeignKeys();
329 5
        if (!isset($parentKeys[$childAlias])) {
330 1
            throw new MapperException(sprintf('Unknown alias *%s* in mapper for parent *%s*', $childAlias, $parentAlias));
331
        }
332 5
        $parentKey = $parentKeys[$childAlias];
333 5
        $parentRelations = $parentRecord->getMapper()->getRelations();
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $parentRecord does not seem to be defined for all execution paths leading up to this point.
Loading history...
334 5
        if (empty($parentRelations[$parentKey->getLocalEntryKey()])) {
335
            throw new MapperException(sprintf('Unknown relation key *%s* in mapper for parent *%s*', $parentKey->getLocalEntryKey(), $parentAlias));
336
        }
337
338 5
        $childTableAlias = empty($customAlias) ? $childAlias : $customAlias;
339 5
        $childLookup = $this->recordLookup($childTableAlias, $childAlias);
340 5
        if (empty($childLookup) || empty($childLookup->getRecord())) {
341
            throw new MapperException(sprintf('Unknown record for child alias *%s*', $childAlias));
342
        }
343 5
        $childRecord = $childLookup->getRecord();
344 5
        $childRelations = $childRecord->getMapper()->getRelations();
345 5
        if (empty($childRelations[$parentKey->getRemoteEntryKey()])) {
346
            throw new MapperException(sprintf('Unknown relation key *%s* in mapper for child *%s*', $parentKey->getRemoteEntryKey(), $childAlias));
347
        }
348
349 5
        $this->queryBuilder->addJoin(
350
            $childAlias,
351 5
            $childRecord->getMapper()->getAlias(),
352 5
            $childRelations[$parentKey->getRemoteEntryKey()],
353
            $parentAlias,
354 5
            $parentRelations[$parentKey->getLocalEntryKey()],
355
            $joinType,
356
            $childTableAlias
357
        );
358
359 4
        return $this;
360
    }
361
362
    /**
363
     * That child is not set for chosen parent
364
     * @param string $childAlias
365
     * @param string $table
366
     * @param string $column
367
     * @param string $parentAlias
368
     * @throws MapperException
369
     * @return $this
370
     */
371 2
    public function childNotExist(string $childAlias, string $table, string $column, string $parentAlias = ''): self
372
    {
373 2
        $this->child($childAlias, IQueryBuilder::JOIN_LEFT_OUTER, $parentAlias);
374 1
        $aTable = $this->correctTable($table);
375 1
        $this->queryBuilder->addCondition(
376
            $aTable,
377 1
            $this->correctColumn($aTable, $column),
378
            IQueryBuilder::OPERATION_NULL
379
        );
380 1
        return $this;
381
    }
382
383
    /**
384
     * Return count of all records selected by params
385
     * @throws MapperException
386
     * @return int
387
     */
388
    abstract public function getCount(): int;
389
390
    /**
391
     * Return records
392
     * @throws MapperException
393
     * @return ARecord[]
394
     */
395
    abstract public function getResults(): array;
396
397
398 7
    protected function correctTable(string $table): string
399
    {
400 7
        return empty($table) ? $this->basicRecord->getMapper()->getAlias() : $table ;
401
    }
402
403
    /**
404
     * @param string $table
405
     * @param string $column
406
     * @throws MapperException
407
     * @return string|int
408
     */
409 7
    protected function correctColumn(string $table, string $column)
410
    {
411 7
        $record = !empty($table) ? $this->recordLookup($table)->getRecord() : $this->basicRecord ;
412 7
        $relations = $record->getMapper()->getRelations();
0 ignored issues
show
Bug introduced by
The method getMapper() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

412
        $relations = $record->/** @scrutinizer ignore-call */ getMapper()->getRelations();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
413 7
        if (empty($relations[$column])) {
414 1
            throw new MapperException(sprintf('Unknown relation key *%s* in mapper for table *%s*', $column, $table));
415
        }
416 6
        return $relations[$column];
417
    }
418
}
419