Passed
Pull Request — master (#16)
by Fabrice
04:30 queued 02:18
created

UniqueKeyExtractor::setExtractQuery()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 9
rs 10
1
<?php
2
3
/*
4
 * This file is part of YaEtl
5
 *     (c) Fabrice de Stefanis / https://github.com/fab2s/YaEtl
6
 * This source file is licensed under the MIT license which you will
7
 * find in the LICENSE file or at https://opensource.org/licenses/MIT
8
 */
9
10
namespace fab2s\YaEtl\Laravel\Extractors;
11
12
use fab2s\NodalFlow\NodalFlowException;
13
use fab2s\NodalFlow\YaEtlException;
14
use fab2s\YaEtl\Extractors\PaginatedQueryInterface;
15
use fab2s\YaEtl\Extractors\PdoUniqueKeyExtractor;
16
use fab2s\YaEtl\Extractors\UniqueKeyExtractorAbstract;
17
use Illuminate\Database\Query\Builder;
0 ignored issues
show
Bug introduced by
The type Illuminate\Database\Query\Builder 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...
18
19
/**
20
 * Class UniqueKeyExtractor
21
 */
22
class UniqueKeyExtractor extends PdoUniqueKeyExtractor implements PaginatedQueryInterface
23
{
24
    use DelayedExtractQueryTrait;
25
26
    /**
27
     * Generic extraction from tables with unique (composite) key
28
     *
29
     * @param Builder      $extractQuery
30
     * @param array|string $uniqueKey    can be either a unique key name as
31
     *                                   string ('id' by default, will be ordered asc) or an associative array :
32
     *                                   [
33
     *                                   'uniqueKeyName' => 'order', // eg 'asc' or 'desc'
34
     *                                   ]
35
     *                                   or, for a unique composite key :
36
     *                                   [
37
     *                                   'compositeKey1' => 'asc',
38
     *                                   'compositeKey2' => 'desc',
39
     *                                   // ...
40
     *                                   ]
41
     *
42
     * @throws YaEtlException
43
     * @throws NodalFlowException
44
     */
45
    public function __construct(?Builder $extractQuery = null, $uniqueKey = 'id')
46
    {
47
        if ($extractQuery !== null) {
48
            $this->setExtractQuery($extractQuery);
49
        }
50
51
        // delay configuring pdo to flow start
52
        UniqueKeyExtractorAbstract::__construct(null, $uniqueKey);
53
    }
54
55
    /**
56
     * This method sets offset and limit in the query
57
     *
58
     * @return string the paginated query with current offset and limit
59
     */
60
    public function getPaginatedQuery(): string
61
    {
62
        if ($this->joinFrom) {
63
            $extractQuery = $this->extractQuery
64
                ->whereIn($this->uniqueKeyName, $this->uniqueKeyValues);
65
        } else {
66
            $extractQuery = $this->extractQuery
67
                ->offset($this->offset)
68
                ->limit($this->batchSize);
69
        }
70
71
        $this->queryBindings = $extractQuery->getRawBindings();
72
        $this->queryBindings = $this->queryBindings['where'];
73
74
        return $extractQuery->toSql();
75
    }
76
}
77