Passed
Pull Request — master (#15)
by Serhii
06:06
created

PullFromSource::handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Matchish\ScoutElasticSearch\Jobs\Stages;
4
5
use Laravel\Scout\Searchable;
6
use Illuminate\Support\Collection;
7
use Illuminate\Database\Eloquent\Model;
8
use Illuminate\Database\Eloquent\Builder;
9
10
/**
11
 * @internal
12
 */
13
final class PullFromSource
14
{
15
    const DEFAULT_CHUNK_SIZE = 500;
16
17
    /**
18
     * @var Builder
19
     */
20
    private $query;
21
22
    /**
23
     * @param Builder $query
24
     */
25 15
    public function __construct(Builder $query)
26
    {
27 15
        $this->query = $query;
28 15
    }
29
30 14
    public function handle(): void
31
    {
32 14
        $results = $this->query->get();
33 14
        $results->filter->shouldBeSearchable()->searchable();
34 14
    }
35
36 8
    public function estimate(): int
37
    {
38 8
        return 1;
39
    }
40
41 8
    public function title(): string
42
    {
43 8
        return 'Indexing...';
44
    }
45
46
    /**
47
     * @param Model $searchable
48
     * @return Collection
49
     */
50 15
    public static function chunked(Model $searchable): Collection
51
    {
52
        /** @var Searchable $searchable */
53 15
        $softDelete = config('scout.soft_delete', false);
54 15
        $query = $searchable->newQuery()
0 ignored issues
show
Bug introduced by
It seems like newQuery() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

54
        $query = $searchable->/** @scrutinizer ignore-call */ newQuery()
Loading history...
55
            ->when($softDelete, function ($query) {
56 1
                return $query->withTrashed();
57 15
            })
58 15
            ->orderBy($searchable->getKeyName());
0 ignored issues
show
Bug introduced by
The method getKeyName() does not exist on Laravel\Scout\Searchable. Did you maybe mean getScoutKeyName()? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

58
            ->orderBy($searchable->/** @scrutinizer ignore-call */ getKeyName());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
59 15
        $totalSearchables = $query->count();
60 15
        if ($totalSearchables) {
61 11
            $chunkSize = (int) config('scout.chunk.searchable', self::DEFAULT_CHUNK_SIZE);
62 11
            $totalChunks = (int) ceil($totalSearchables / $chunkSize);
63
64
            return collect(range(1, $totalChunks))->map(function ($page) use ($query, $chunkSize) {
65 11
                $clone = (clone $query)->forPage($page, $chunkSize);
66
67 11
                return new static($clone);
68 11
            });
69
        } else {
70 10
            return collect();
71
        }
72
    }
73
}
74