Passed
Pull Request — master (#33)
by Serhii
05:37
created

ChunkScope   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A key() 0 3 1
A apply() 0 10 1
1
<?php
2
3
namespace Matchish\ScoutElasticSearch\Database\Scopes;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Database\Eloquent\Scope;
7
use Illuminate\Database\Eloquent\Builder;
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 12
    public function __construct($start, $end)
26
    {
27 12
        $this->start = $start;
28 12
        $this->end = $end;
29 12
    }
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 11
    public function apply(Builder $builder, Model $model)
39
    {
40 11
        $start = $this->start;
41 11
        $end = $this->end;
42
        $builder
43
            ->when(! is_null($start), function ($query) use ($start, $model) {
44 9
                return $query->where($model->getKeyName(), '>', $start);
45 11
            })
46
            ->when(! is_null($end), function ($query) use ($end, $model) {
47 11
                return $query->where($model->getKeyName(), '<=', $end);
48 11
            });
49 11
    }
50
51 11
    public function key()
52
    {
53 11
        return static::class;
54
    }
55
}
56