Completed
Pull Request — master (#3)
by Denis
01:50 queued 23s
created

EloquentDataProvider   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A getCount() 0 4 1
A getData() 0 18 4
1
<?php
2
3
namespace Woo\GridView\DataProviders;
4
5
use Illuminate\Database\Eloquent\Builder;
6
7
class EloquentDataProvider extends BaseDataProvider
8
{
9
    protected $query;
10
11
    /**
12
     * EloquentDataProvider constructor.
13
     * @param Builder $query
14
     */
15
    public function __construct(Builder $query)
16
    {
17
        $this->query = clone $query;
18
    }
19
20
    /**
21
     * @inheritdoc
22
     */
23
    public function getCount(): int
24
    {
25
        return $this->query->count();
26
    }
27
28
    /**
29
     * @inheritdoc
30
     */
31
    public function getData(array $filters, string $orderBy, string $orderSort, int $page, int $perPage)
32
    {
33
        $query = clone $this->query;
34
35
        foreach ($filters as $field => $value) {
36
            $query->where($field, 'LIKE', '%' . $value . '%');
37
        }
38
39
        if ($orderBy) {
40
            $query->orderBy($orderBy, $orderSort);
41
        }
42
43
        if ($perPage == 0) {
44
            return $query->get();
45
        }
46
47
        return $query->offset(($page - 1) * $perPage)->limit($perPage)->get();
48
    }
49
}