Completed
Branch master (6f5620)
by Anton
02:08
created

SelectSource   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 92
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 85.71%

Importance

Changes 0
Metric Value
dl 0
loc 92
ccs 30
cts 35
cp 0.8571
rs 10
c 0
b 0
f 0
wmc 10
lcom 1
cbo 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setSource() 0 9 2
B process() 0 59 8
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\Proxy;
16
17
/**
18
 * SQL Source Adapter for Grid package
19
 *
20
 * @package  Bluz\Grid
21
 * @author   Anton Shevchuk
22
 */
23
class SelectSource extends AbstractSource
24
{
25
    /**
26
     * @var Db\Query\Select instance of select source
27
     */
28
    protected $source;
29
30
    /**
31
     * Set Select source
32
     *
33
     * @param  Db\Query\Select $source
34
     *
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
     *
53
     * @return \Bluz\Grid\Data
54
     */
55 1
    public function process(array $settings = [])
56
    {
57
        // process filters
58 1
        if (!empty($settings['filters'])) {
59 1
            foreach ($settings['filters'] as $column => $filters) {
60 1
                foreach ($filters as $filter => $value) {
61 1
                    if ($filter === Grid\Grid::FILTER_LIKE) {
62 1
                        $value = '%' . $value . '%';
63
                    }
64 1
                    $this->source->andWhere($column . ' ' . $this->filters[$filter] . ' ?', $value);
65
                }
66
            }
67
        }
68
69
        // process orders
70 1
        if (!empty($settings['orders'])) {
71
            // Obtain a list of columns
72
            foreach ($settings['orders'] as $column => $order) {
73
                $this->source->addOrderBy($column, $order);
74
            }
75
        }
76
77
        // process pages
78 1
        $this->source->setLimit($settings['limit']);
79 1
        $this->source->setPage($settings['page']);
80
81
        // prepare query
82 1
        $type = Proxy\Db::getOption('connect', 'type');
83
84 1
        if (strtolower($type) === 'mysql') {
85
            // MySQL
86 1
            $select = $this->source->getQueryPart('select');
87 1
            $this->source->select('SQL_CALC_FOUND_ROWS ' . current($select));
88
89 1
            $totalSql = 'SELECT FOUND_ROWS()';
90
        } else {
91
            // other
92
            $totalSource = clone $this->source;
93
            $totalSource->select('COUNT(*)');
94
95
            $totalSql = $totalSource->getSql();
96
        }
97
98 1
        $data = [];
99 1
        $total = 0;
100
101
        // run queries
102
        // use transaction to avoid errors
103 1
        Proxy\Db::transaction(
104 1
            function () use (&$data, &$total, $totalSql) {
105 1
                $data = $this->source->execute();
106 1
                $total = (int)Proxy\Db::fetchOne($totalSql);
107 1
            }
108
        );
109
110 1
        $gridData = new Grid\Data($data);
111 1
        $gridData->setTotal($total);
112 1
        return $gridData;
113
    }
114
}
115