Test Failed
Pull Request — master (#33)
by Serhii
07:11 queued 03:16
created

DefaultImportSource::model()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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