Passed
Push — master ( cb2842...994f5c )
by Patrick
02:04
created

TrapDirectorTableOrder   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 24
c 1
b 0
f 0
dl 0
loc 57
rs 10
wmc 12

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setOrder() 0 4 1
A curOrderQuery() 0 4 2
A getOrderQuery() 0 11 3
A isOrderSet() 0 4 2
A applyOrder() 0 16 4
1
<?php
2
3
namespace Icinga\Module\Trapdirector\Tables;
4
5
6
trait TrapDirectorTableOrder
7
{ 
8
    /** @var array $order : (db column, 'ASC' | 'DESC') */
9
    protected $order = array();
10
    protected $orderQuery = '';   
11
    
12
   /***************** Ordering ********************/
13
    
14
    public function applyOrder()
15
    {
16
        if (count($this->order) == 0)
17
        {
18
            return $this;
19
        }
20
        $orderSQL='';
21
        foreach ($this->order as $column => $direction)
22
        {
23
            if ($orderSQL != "") $orderSQL.=',';
24
            
25
            $orderSQL .= $column . ' ' . $direction;
26
        }
27
        $this->query = $this->query->order($orderSQL);
0 ignored issues
show
Bug Best Practice introduced by
The property query does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
28
        
29
        return $this;
30
    }
31
    
32
    public function setOrder(array $order)
33
    {
34
        $this->order = $order;
35
        return $this;
36
    }
37
38
    public function isOrderSet()
39
    {
40
        if (count($this->order) == 0) return FALSE;
41
        return TRUE;
42
    }
43
    
44
    public function getOrderQuery(array $getVars)
45
    {
46
        if (isset($getVars['o']))
47
        {
48
            $this->orderQuery = $getVars['o'];
49
            $match = array();
50
            if (preg_match('/(.*)(ASC|DESC)$/', $this->orderQuery , $match))
51
            {
52
                $orderArray=array($match[1] => $match[2]);
53
                echo "$match[1] => $match[2]";
54
                $this->setOrder($orderArray);
55
            }
56
        }
57
    }
58
    
59
    protected function curOrderQuery()
60
    {
61
        if ($this->orderQuery == '') return '';
62
        return 'o='.$this->orderQuery;
63
    }
64
        
65
}