Passed
Push — master ( b01856...621a27 )
by Alexander
02:26 queued 11s
created

DataProvider   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 26.09%

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 3
dl 0
loc 47
ccs 6
cts 23
cp 0.2609
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A prepareTotalCount() 0 4 1
A prepareModels() 0 20 4
A prepareKeys() 0 4 1
1
<?php declare(strict_types=1);
2
3
namespace Indigerd\Repository\DataProvider;
4
5
use Indigerd\Repository\Repository;
6
use yii\data\BaseDataProvider;
7
8
class DataProvider extends BaseDataProvider
9
{
10
    protected $repository;
11
12
    protected $conditions;
13
14
    protected $with;
15
16 1
    public function __construct(Repository $repository, array $conditions = [], $with = [], array $config = [])
17
    {
18 1
        $this->repository = $repository;
19 1
        $this->conditions = $conditions;
20 1
        $this->with = $with;
21 1
        parent::__construct($config);
22 1
    }
23
24
    protected function prepareTotalCount()
25
    {
26
        return $this->repository->aggregateCount('', $this->conditions);
27
    }
28
29
    protected function prepareModels()
30
    {
31
        $limit = 0;
32
        $offset = 0;
33
        $orderBy = [];
34
35
        if (($pagination = $this->getPagination()) !== false) {
36
            $pagination->totalCount = $this->getTotalCount();
37
            if ($pagination->totalCount === 0) {
38
                return [];
39
            }
40
            $limit = $pagination->getLimit();
41
            $offset = $pagination->getOffset();
42
        }
43
        if (($sort = $this->getSort()) !== false) {
44
            $orderBy = $sort->getOrders();
45
        }
46
47
        return $this->repository->findAll($this->conditions, $orderBy, $limit, $offset, $this->with);
48
    }
49
50
    protected function prepareKeys($models)
51
    {
52
        return \array_keys($models);
53
    }
54
}
55