Passed
Push — master ( b7e61c...9eb859 )
by Peter
02:42
created

RepoGridAbstract   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 25
dl 0
loc 101
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getSortConditions() 0 3 1
A getOffset() 0 6 1
A getSqlParams() 0 3 1
A __construct() 0 10 1
A createAndPopulate() 0 17 1
A getWhereConditions() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AbterPhp\Admin\Service\RepoGrid;
6
7
use AbterPhp\Framework\Databases\Queries\FoundRows;
8
use AbterPhp\Framework\Grid\Factory\IBase as GridFactory;
9
use AbterPhp\Framework\Grid\IGrid;
10
use AbterPhp\Framework\Http\Service\RepoGrid\IRepoGrid;
11
use AbterPhp\Framework\Orm\IGridRepo;
12
use Casbin\Enforcer;
13
use Opulence\Http\Collection;
14
15
abstract class RepoGridAbstract implements IRepoGrid
16
{
17
    /** @var Enforcer */
18
    protected $enforcer;
19
20
    /** @var IGridRepo */
21
    protected $repo;
22
23
    /** @var FoundRows */
24
    protected $foundRows;
25
26
    /** @var GridFactory */
27
    protected $gridFactory;
28
29
    /**
30
     * GridAbstract constructor.
31
     *
32
     * @param Enforcer          $enforcer
33
     * @param IGridRepo         $repo
34
     * @param FoundRows         $foundRows
35
     * @param GridFactory       $gridFactory
36
     */
37
    public function __construct(
38
        Enforcer $enforcer,
39
        IGridRepo $repo,
40
        FoundRows $foundRows,
41
        GridFactory $gridFactory
42
    ) {
43
        $this->enforcer          = $enforcer;
44
        $this->repo              = $repo;
45
        $this->foundRows         = $foundRows;
46
        $this->gridFactory       = $gridFactory;
47
    }
48
49
    /**
50
     * @param Collection $query
51
     * @param string     $baseUrl
52
     *
53
     * @return IGrid
54
     */
55
    public function createAndPopulate(Collection $query, string $baseUrl): IGrid
56
    {
57
        $grid = $this->gridFactory->createGrid($query->getAll(), $baseUrl);
58
59
        $pageSize  = $grid->getPageSize();
60
        $limitFrom = $this->getOffset($query, $pageSize);
61
62
        $sortBy = $this->getSortConditions($grid);
63
        $where  = $this->getWhereConditions($grid);
64
        $params = $this->getSqlParams($grid);
65
66
        $entities = $this->repo->getPage($limitFrom, $pageSize, $sortBy, $where, $params);
67
        $maxCount = $this->foundRows->get();
68
69
        $grid->setTotalCount($maxCount)->setEntities($entities);
70
71
        return $grid;
72
    }
73
74
    /**
75
     * @param IGrid $grid
76
     *
77
     * @return array
78
     */
79
    protected function getSortConditions(IGrid $grid): array
80
    {
81
        return $grid->getSortConditions();
82
    }
83
84
    /**
85
     * @param IGrid $grid
86
     *
87
     * @return array
88
     */
89
    protected function getWhereConditions(IGrid $grid): array
90
    {
91
        return $grid->getWhereConditions();
92
    }
93
94
    /**
95
     * @param IGrid $grid
96
     *
97
     * @return array
98
     */
99
    protected function getSqlParams(IGrid $grid): array
100
    {
101
        return $grid->getSqlParams();
102
    }
103
104
    /**
105
     * @param Collection $query
106
     * @param int        $pageSize
107
     *
108
     * @return int
109
     */
110
    protected function getOffset(Collection $query, int $pageSize): int
111
    {
112
        $page   = (int)$query->get('page', 1);
113
        $offset = ($page - 1) * $pageSize;
114
115
        return $offset;
116
    }
117
}
118