Passed
Pull Request — master (#33)
by Serhii
05:37
created

DefaultImportSource::chunked()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 31
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 23
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 24
nc 4
nop 0
dl 0
loc 31
ccs 23
cts 23
cp 1
crap 4
rs 9.536
c 0
b 0
f 0
1
<?php
2
3
namespace Matchish\ScoutElasticSearch\Searchable;
4
5
use Matchish\ScoutElasticSearch\Database\Scopes\ChunkScope;
6
7
class DefaultImportSource implements ImportSource
8
{
9
    const DEFAULT_CHUNK_SIZE = 500;
10
11
    private $className;
12
    private $scopes;
13
14
    /**
15
     * DefaultImportSource constructor.
16
     * @param string $className
17
     * @param array $scopes
18
     */
19 25
    public function __construct(string $className, array $scopes = [])
20
    {
21 25
        $this->className = $className;
22 25
        $this->scopes = $scopes;
23 25
    }
24
25 11
    public function syncWithSearchUsingQueue()
26
    {
27 11
        return $this->model()->syncWithSearchUsingQueue();
28
    }
29
30 11
    public function syncWithSearchUsing()
31
    {
32 11
        return $this->model()->syncWithSearchUsing();
33
    }
34
35 18
    public function searchableAs(): string
36
    {
37 18
        return $this->model()->searchableAs();
38
    }
39
40 17
    public function chunked()
41
    {
42 17
        $query = $this->newQuery();
43 17
        $searchable = $this->model();
44 17
        $totalSearchables = $query->count();
45 17
        if ($totalSearchables) {
46 12
            $chunkSize = (int) config('scout.chunk.searchable', self::DEFAULT_CHUNK_SIZE);
47 12
            $cloneQuery = clone $query;
48 12
            $cloneQuery->joinSub('SELECT @row :=0, 1 as temp', 'r', 'r.temp', 'r.temp')
49 12
                ->selectRaw("@row := @row +1 AS rownum, {$searchable->getKeyName()}");
50 12
            $ids = \DB::query()->fromSub($cloneQuery, 'ranked')->whereRaw("rownum %{$chunkSize} =0")->pluck('id');
51 12
            $pairs = [];
52 12
            $lastId = null;
53 12
            foreach ($ids as $id) {
54 12
                if ($lastId) {
55 5
                    $pairs[] = [$lastId, $id];
56
                } else {
57 12
                    $pairs[] = [null, $id];
58
                }
59 12
                $lastId = $id;
60
            }
61 12
            $pairs[] = [$lastId, null];
62
63
            return collect($pairs)->map(function ($pair) {
64 12
                [$start, $end] = $pair;
65 12
                $chunkScope = new ChunkScope($start, $end);
66
67 12
                return new static($this->className, array_merge($this->scopes, [$chunkScope]));
68 12
            });
69
        } else {
70 12
            return collect();
71
        }
72
    }
73
74
    /**
75
     * @return mixed
76
     */
77 25
    private function model()
78
    {
79 25
        return new $this->className;
80
    }
81
82 21
    private function newQuery()
83
    {
84 21
        $query = $this->model()->newQuery();
85 21
        $softDelete = config('scout.soft_delete', false);
86
        $query
87
            ->when($softDelete, function ($query) {
88 2
                return $query->withTrashed();
89 21
            })
90 21
            ->orderBy($this->model()->getKeyName());
91 21
        $scopes = $this->scopes;
92
93
        return collect($scopes)->reduce(function ($instance, $scope) {
94 11
            $instance->withGlobalScope($scope->key(), $scope);
95
96 11
            return $instance;
97 21
        }, $query);
98
    }
99
100 15
    public function get()
101
    {
102 15
        return $this->newQuery()->get();
103
    }
104
}
105