Passed
Push — master ( 4961e1...3955ab )
by Peter
02:28
created

RepoGridAbstract::getSqlParams()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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