Completed
Push — master ( 467d47...6f2bb4 )
by Anton
14s
created

SelectSource::applyFilters()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 0
Metric Value
nc 4
dl 0
loc 11
ccs 7
cts 7
cp 1
c 0
b 0
f 0
cc 4
eloc 6
nop 1
crap 4
rs 9.2
1
<?php
2
/**
3
 * Bluz Framework Component
4
 *
5
 * @copyright Bluz PHP Team
6
 * @link      https://github.com/bluzphp/framework
7
 */
8
9
declare(strict_types=1);
10
11
namespace Bluz\Grid\Source;
12
13
use Bluz\Db;
14
use Bluz\Grid;
15
use Bluz\Grid\Data;
16
use Bluz\Proxy;
17
18
/**
19
 * SQL Source Adapter for Grid package
20
 *
21
 * @package  Bluz\Grid
22
 * @author   Anton Shevchuk
23
 *
24
 * @method   Db\Query\Select getSource()
25
 */
26
class SelectSource extends AbstractSource
27
{
28
    /**
29
     * Set Select source
30
     *
31
     * @param  Db\Query\Select $source
32
     *
33
     * @throws Grid\GridException
34
     * @return void
35
     */
36 2
    public function setSource($source) : void
37
    {
38 2
        if (!$source instanceof Db\Query\Select) {
39 1
            throw new Grid\GridException('Source of `SelectSource` should be `Db\\Query\\Select` object');
40
        }
41 1
        parent::setSource($source);
42 1
    }
43
44
    /**
45
     * {@inheritdoc}
46
     */
47 1
    public function process(int $page, int $limit, array $filters = [], array $orders = []) : Data
48
    {
49
        // process filters
50 1
        $this->applyFilters($filters);
51
52
        // process orders
53 1
        $this->applyOrders($orders);
54
55
        // process pages
56 1
        $this->getSource()->setLimit($limit);
57 1
        $this->getSource()->setPage($page);
58
59
        // prepare query
60 1
        $type = Proxy\Db::getOption('connect', 'type');
61
62 1
        if (strtolower($type) === 'mysql') {
63
            // MySQL
64 1
            $selectPart = $this->getSource()->getSelect();
65 1
            $selectPart[0] = 'SQL_CALC_FOUND_ROWS ' . $selectPart[0];
66 1
            $this->getSource()->select(...$selectPart);
67
68 1
            $totalSql = 'SELECT FOUND_ROWS()';
69
        } else {
70
            // other
71
            $totalSource = clone $this->getSource();
72
            $totalSource->select('COUNT(*)');
73
74
            $totalSql = $totalSource->getSql();
75
        }
76
77 1
        $data = [];
78 1
        $total = 0;
79
80
        // run queries
81
        // use transaction to avoid errors
82 1
        Proxy\Db::transaction(
83 1
            function () use (&$data, &$total, $totalSql) {
84 1
                $data = $this->source->execute();
85 1
                $total = (int)Proxy\Db::fetchOne($totalSql);
86 1
            }
87
        );
88
89 1
        $gridData = new Data($data);
90 1
        $gridData->setTotal($total);
91 1
        return $gridData;
92
    }
93
94
    /**
95
     * Apply filters to Select
96
     *
97
     * @param  array $settings
98
     *
99
     * @return void
100
     */
101 1
    private function applyFilters(array $settings) : void
102
    {
103 1
        foreach ($settings as $column => $filters) {
104 1
            foreach ($filters as $filter => $value) {
105 1
                if ($filter === Grid\Grid::FILTER_LIKE) {
106 1
                    $value = '%' . $value . '%';
107
                }
108 1
                $this->getSource()->andWhere($column . ' ' . $this->filters[$filter] . ' ?', $value);
109
            }
110
        }
111 1
    }
112
113
    /**
114
     * Apply order to Select
115
     *
116
     * @param  array $settings
117
     *
118
     * @return void
119
     */
120 1
    private function applyOrders(array $settings) : void
121
    {
122
        // Obtain a list of columns
123 1
        foreach ($settings as $column => $order) {
124
            $this->getSource()->addOrderBy($column, $order);
125
        }
126 1
    }
127
}
128