RepoGridAbstract   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 96
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 6
eloc 24
c 0
b 0
f 0
dl 0
loc 96
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A getSortConditions() 0 3 1
A getOffset() 0 5 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
    protected Enforcer $enforcer;
18
19
    protected IGridRepo $repo;
20
21
    protected FoundRows $foundRows;
22
23
    protected GridFactory $gridFactory;
24
25
    /**
26
     * GridAbstract constructor.
27
     *
28
     * @param Enforcer          $enforcer
29
     * @param IGridRepo         $repo
30
     * @param FoundRows         $foundRows
31
     * @param GridFactory       $gridFactory
32
     */
33
    public function __construct(
34
        Enforcer $enforcer,
35
        IGridRepo $repo,
36
        FoundRows $foundRows,
37
        GridFactory $gridFactory
38
    ) {
39
        $this->enforcer          = $enforcer;
40
        $this->repo              = $repo;
41
        $this->foundRows         = $foundRows;
42
        $this->gridFactory       = $gridFactory;
43
    }
44
45
    /**
46
     * @param Collection $query
47
     * @param string     $baseUrl
48
     *
49
     * @return IGrid
50
     */
51
    public function createAndPopulate(Collection $query, string $baseUrl): IGrid
52
    {
53
        $grid = $this->gridFactory->createGrid($query->getAll(), $baseUrl);
54
55
        $pageSize  = $grid->getPageSize();
56
        $limitFrom = $this->getOffset($query, $pageSize);
57
58
        $sortBy = $this->getSortConditions($grid);
59
        $where  = $this->getWhereConditions($grid);
60
        $params = $this->getSqlParams($grid);
61
62
        $entities = $this->repo->getPage($limitFrom, $pageSize, $sortBy, $where, $params);
63
        $maxCount = $this->foundRows->get();
64
65
        $grid->setTotalCount($maxCount)->setEntities($entities);
66
67
        return $grid;
68
    }
69
70
    /**
71
     * @param IGrid $grid
72
     *
73
     * @return array
74
     */
75
    protected function getSortConditions(IGrid $grid): array
76
    {
77
        return $grid->getSortConditions();
78
    }
79
80
    /**
81
     * @param IGrid $grid
82
     *
83
     * @return array
84
     */
85
    protected function getWhereConditions(IGrid $grid): array
86
    {
87
        return $grid->getWhereConditions();
88
    }
89
90
    /**
91
     * @param IGrid $grid
92
     *
93
     * @return array
94
     */
95
    protected function getSqlParams(IGrid $grid): array
96
    {
97
        return $grid->getSqlParams();
98
    }
99
100
    /**
101
     * @param Collection $query
102
     * @param int        $pageSize
103
     *
104
     * @return int
105
     */
106
    protected function getOffset(Collection $query, int $pageSize): int
107
    {
108
        $page   = (int)$query->get('page', 1);
109
110
        return ($page - 1) * $pageSize;
111
    }
112
}
113