Passed
Pull Request — master (#33)
by Serhii
04:16
created

ChunkScope   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 45
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

3 Methods

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