ChunkScope   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
eloc 13
dl 0
loc 45
ccs 0
cts 14
cp 0
rs 10
c 0
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A key() 0 3 1
A __construct() 0 4 1
A apply() 0 10 1
1
<?php
2
3
namespace Matchish\ScoutElasticSearch\Database\Scopes;
4
5
use Illuminate\Database\Eloquent\Builder;
6
use Illuminate\Database\Eloquent\Model;
7
use Illuminate\Database\Eloquent\Scope;
8
9
class ChunkScope implements Scope
10
{
11
    /**
12
     * @var mixed
13
     */
14
    private $start;
15
    /**
16
     * @var mixed
17
     */
18
    private $end;
19
20
    /**
21
     * ChunkScope constructor.
22
     * @param mixed $start
23
     * @param mixed $end
24
     */
25
    public function __construct($start, $end)
26
    {
27
        $this->start = $start;
28
        $this->end = $end;
29
    }
30
31
    /**
32
     * Apply the scope to a given Eloquent query builder.
33
     *
34
     * @param  \Illuminate\Database\Eloquent\Builder  $builder
35
     * @param  \Illuminate\Database\Eloquent\Model  $model
36
     * @return void
37
     */
38
    public function apply(Builder $builder, Model $model)
39
    {
40
        $start = $this->start;
41
        $end = $this->end;
42
        $builder
43
            ->when(! is_null($start), function ($query) use ($start, $model) {
44
                return $query->where($model->getKeyName(), '>', $start);
45
            })
46
            ->when(! is_null($end), function ($query) use ($end, $model) {
47
                return $query->where($model->getKeyName(), '<=', $end);
48
            });
49
    }
50
51
    public function key(): string
52
    {
53
        return static::class;
54
    }
55
}
56