OffsetLimitScope   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 36
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 1
dl 0
loc 36
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B apply() 0 27 6
1
<?php
2
3
namespace Goopil\RestFilter\Scopes;
4
5
use Illuminate\Database\Eloquent\Model;
6
use Illuminate\Database\Eloquent\Builder;
7
8
/**
9
 * Class OffsetLimitScope.
10
 */
11
class OffsetLimitScope extends BaseScope
12
{
13
    /**
14
     * @param Builder $builder
15
     * @param Model   $model
16
     *
17
     * @return Builder
18
     */
19
    public function apply(Builder $builder, Model $model)
20
    {
21
        $hasLimit = false;
22
23
        $limitKey = config('queryScope.offsetLimit.limitParam', 'limit');
24
        if ($this->request->has($limitKey)) {
25
            $value = $this->request->get($limitKey);
26
            $builder = $builder->limit(is_int($value) ? $value : 15);
27
            $hasLimit = true;
28
        }
29
30
        $offsetKey = config('queryScope.offsetLimit.offsetParam', 'offset');
31
        if ($this->request->has($offsetKey)) {
32
            $value = $this->request->get($offsetKey);
33
            $builder = $builder->offset(is_int($value) ? $value : 0);
34
35
            /*
36
             * nasty work around ...
37
             * @see https://github.com/laravel/framework/issues/5458
38
             */
39
            if ($hasLimit === false) {
40
                $builder = $builder->limit(PHP_INT_MAX);
41
            }
42
        }
43
44
        return $builder;
45
    }
46
}
47