Test Failed
Pull Request — master (#161)
by
unknown
06:47
created

DefaultImportSource::chunked()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 10
nc 2
nop 0
dl 0
loc 15
ccs 11
cts 11
cp 1
crap 2
rs 9.9332
c 1
b 0
f 0
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 16
    public function __construct(string $className, array $scopes = [])
29
    {
30 16
        $this->className = $className;
31 16
        $this->scopes = $scopes;
32 16
    }
33
34
    public function syncWithSearchUsingQueue(): ?string
35
    {
36
        return $this->model()->syncWithSearchUsingQueue();
37
    }
38
39
    public function syncWithSearchUsing(): ?string
40
    {
41
        return $this->model()->syncWithSearchUsing();
42
    }
43
44 8
    public function searchableAs(): string
45
    {
46 8
        return $this->model()->searchableAs();
47
    }
48
49 6
    public function chunked(): Collection
50
    {
51 6
        $query = $this->newQuery();
52 6
        $totalSearchables = $query->count();
53 6
        if ($totalSearchables) {
54 4
            $chunkSize = (int) config('scout.chunk.searchable', self::DEFAULT_CHUNK_SIZE);
55 4
            $totalChunks = (int) ceil($totalSearchables / $chunkSize);
56
57 4
            return collect(range(1, $totalChunks))->map(function ($page) use ($chunkSize) {
58 4
                $chunkScope = new PageScope($page, $chunkSize);
59
60 4
                return new static($this->className, array_merge($this->scopes, [$chunkScope]));
61 4
            });
62
        } else {
63 2
            return collect();
64
        }
65
    }
66
67
    /**
68
     * @return mixed
69
     */
70 16
    private function model()
71
    {
72 16
        return new $this->className;
73
    }
74
75 11
    private function newQuery(): Builder
76
    {
77 11
        $query = $this->model()->newQuery();
78 11
        $softDelete = $this->className::usesSoftDelete() && config('scout.soft_delete', false);
79
        $query
80 11
            ->when($softDelete, function ($query) {
81 2
                return $query->withTrashed();
82 11
            })
83 11
            ->orderBy($this->model()->getKeyName());
84 11
        $scopes = $this->scopes;
85
86 11
        return collect($scopes)->reduce(function ($instance, $scope) {
87 4
            $instance->withGlobalScope(get_class($scope), $scope);
88
89 4
            return $instance;
90 11
        }, $query);
91
    }
92
93 8
    public function get(): EloquentCollection
94
    {
95
        /** @var EloquentCollection $models */
96 8
        $models = $this->newQuery()->get();
97
98 8
        return $models;
99
    }
100
}
101