ZendSelect   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 130
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 9

Importance

Changes 0
Metric Value
wmc 18
lcom 1
cbo 9
dl 0
loc 130
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 2
A setAdapter() 0 10 3
A getData() 0 4 1
A getAdapter() 0 4 1
C execute() 0 66 11
1
<?php
2
3
namespace ZfcDatagrid\DataSource;
4
5
use Zend\Db\Sql;
6
use Zend\Db\Sql\Expression;
7
use Zend\Paginator\Adapter\DbSelect as PaginatorAdapter;
8
use ZfcDatagrid\Column;
9
10
class ZendSelect extends AbstractDataSource
11
{
12
    /**
13
     * @var Sql\Select
14
     */
15
    private $select;
16
17
    /**
18
     * @var \Zend\Db\Sql\Sql
19
     */
20
    private $sqlObject;
21
22
    /**
23
     * Data source.
24
     *
25
     * @param Sql\Select $data
26
     *
27
     * @throws \InvalidArgumentException
28
     */
29
    public function __construct($data)
30
    {
31
        if ($data instanceof Sql\Select) {
32
            $this->select = $data;
33
        } else {
34
            throw new \InvalidArgumentException('A instance of Zend\Db\SqlSelect is needed to use this dataSource!');
35
        }
36
    }
37
38
    /**
39
     * @return Sql\Select
40
     */
41
    public function getData()
42
    {
43
        return $this->select;
44
    }
45
46
    /**
47
     * @param $adapterOrSqlObject
48
     *
49
     * @throws \InvalidArgumentException
50
     */
51
    public function setAdapter($adapterOrSqlObject)
52
    {
53
        if ($adapterOrSqlObject instanceof \Zend\Db\Sql\Sql) {
54
            $this->sqlObject = $adapterOrSqlObject;
55
        } elseif ($adapterOrSqlObject instanceof \Zend\Db\Adapter\Adapter) {
56
            $this->sqlObject = new \Zend\Db\Sql\Sql($adapterOrSqlObject);
57
        } else {
58
            throw new \InvalidArgumentException('Object of "Zend\Db\Sql\Sql" or "Zend\Db\Adapter\Adapter" needed.');
59
        }
60
    }
61
62
    /**
63
     * @return \Zend\Db\Sql\Sql
64
     */
65
    public function getAdapter()
66
    {
67
        return $this->sqlObject;
68
    }
69
70
    /**
71
     * @throws \Exception
72
     */
73
    public function execute()
74
    {
75
        if ($this->getAdapter() === null || !$this->getAdapter() instanceof \Zend\Db\Sql\Sql) {
76
            throw new \Exception('Object "Zend\Db\Sql\Sql" is missing, please call setAdapter() first!');
77
        }
78
79
        $platform = $this->getAdapter()
80
            ->getAdapter()
81
            ->getPlatform();
82
83
        $select = $this->getData();
84
85
        /*
86
         * Step 1) Apply needed columns
87
         */
88
        $selectColumns = [];
89
        foreach ($this->getColumns() as $col) {
90
            if (!$col instanceof Column\Select) {
91
                continue;
92
            }
93
94
            $colString = $col->getSelectPart1();
95
            if ($col->getSelectPart2() != '') {
96
                $colString = new Expression($platform->quoteIdentifier($colString).$platform->getIdentifierSeparator().$platform->quoteIdentifier($col->getSelectPart2()));
97
            }
98
99
            $selectColumns[$col->getUniqueId()] = $colString;
100
        }
101
        $select->columns($selectColumns, false);
102
103
        $joins = $select->getRawState('joins');
104
        $select->reset('joins');
105
        foreach ($joins as $join) {
0 ignored issues
show
Bug introduced by
The expression $joins of type string|array|object<Zend...d\Db\Sql\Where>|integer is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
106
            $select->join($join['name'], $join['on'], [], $join['type']);
107
        }
108
109
        /*
110
         * Step 2) Apply sorting
111
         */
112
        if (!empty($this->getSortConditions())) {
113
            // Minimum one sort condition given -> so reset the default orderBy
114
            $select->reset(Sql\Select::ORDER);
115
116
            foreach ($this->getSortConditions() as $sortCondition) {
117
                /** @var \ZfcDataGrid\Column\AbstractColumn $col */
118
                $col = $sortCondition['column'];
119
                $select->order($col->getUniqueId().' '.$sortCondition['sortDirection']);
120
            }
121
        }
122
123
        /*
124
         * Step 3) Apply filters
125
         */
126
        $filterColumn = new ZendSelect\Filter($this->getAdapter(), $select);
127
        foreach ($this->getFilters() as $filter) {
128
            /* @var $filter \ZfcDatagrid\Filter */
129
            if ($filter->isColumnFilter() === true) {
130
                $filterColumn->applyFilter($filter);
131
            }
132
        }
133
134
        /*
135
         * Step 4) Pagination
136
         */
137
        $this->setPaginatorAdapter(new PaginatorAdapter($select, $this->getAdapter()));
138
    }
139
}
140