ZendSelect::execute()   C
last analyzed

Complexity

Conditions 11
Paths 49

Size

Total Lines 66
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 66
rs 5.9447
c 0
b 0
f 0
cc 11
eloc 30
nc 49
nop 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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