Completed
Pull Request — master (#388)
by Anton
05:43
created

SelectSource::setSource()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

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