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

DataProvider::prepareTotalCount()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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