DefaultImportSource::newQuery()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 11
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 16
ccs 10
cts 10
cp 1
crap 2
rs 9.9
1
<?php
2
3
namespace Matchish\ScoutElasticSearch\Searchable;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
7
use Illuminate\Support\Collection;
8
use Matchish\ScoutElasticSearch\Database\Scopes\PageScope;
9
10
final class DefaultImportSource implements ImportSource
11
{
12
    const DEFAULT_CHUNK_SIZE = 500;
13
14
    /**
15
     * @var string
16
     */
17
    private $className;
18
    /**
19
     * @var array
20
     */
21
    private $scopes;
22
23
    /**
24
     * DefaultImportSource constructor.
25
     * @param string $className
26
     * @param array $scopes
27
     */
28 28
    public function __construct(string $className, array $scopes = [])
29
    {
30 28
        $this->className = $className;
31 28
        $this->scopes = $scopes;
32 28
    }
33
34 12
    public function syncWithSearchUsingQueue(): ?string
35
    {
36 12
        return $this->model()->syncWithSearchUsingQueue();
37
    }
38
39 12
    public function syncWithSearchUsing(): ?string
40
    {
41 12
        return $this->model()->syncWithSearchUsing();
42
    }
43
44 20
    public function searchableAs(): string
45
    {
46 20
        return $this->model()->searchableAs();
47
    }
48
49 18
    public function chunked(): Collection
50
    {
51 18
        $query = $this->newQuery();
52 18
        $totalSearchables = $query->count();
53 18
        if ($totalSearchables) {
54 13
            $chunkSize = (int) config('scout.chunk.searchable', self::DEFAULT_CHUNK_SIZE);
55 13
            $totalChunks = (int) ceil($totalSearchables / $chunkSize);
56
57
            return collect(range(1, $totalChunks))->map(function ($page) use ($chunkSize) {
58 13
                $chunkScope = new PageScope($page, $chunkSize);
59
60 13
                return new static($this->className, array_merge($this->scopes, [$chunkScope]));
61 13
            });
62
        } else {
63 13
            return collect();
64
        }
65
    }
66
67
    /**
68
     * @return mixed
69
     */
70 28
    private function model()
71
    {
72 28
        return new $this->className;
73
    }
74
75 23
    private function newQuery(): Builder
76
    {
77 23
        $query = $this->model()->newQuery();
78 23
        $softDelete = $this->className::usesSoftDelete() && config('scout.soft_delete', false);
79
        $query
80
            ->when($softDelete, function ($query) {
81 2
                return $query->withTrashed();
82 23
            })
83 23
            ->orderBy($this->model()->getQualifiedKeyName());
84 23
        $scopes = $this->scopes;
85
86
        return collect($scopes)->reduce(function ($instance, $scope) {
87 13
            $instance->withGlobalScope(get_class($scope), $scope);
88
89 13
            return $instance;
90 23
        }, $query);
91
    }
92
93 17
    public function get(): EloquentCollection
94
    {
95
        /** @var EloquentCollection $models */
96 17
        $models = $this->newQuery()->get();
97
98 17
        return $models;
99
    }
100
}
101