HandlerTableList::render()   C
last analyzed

Complexity

Conditions 15
Paths 72

Size

Total Lines 100
Code Lines 56

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 15
eloc 56
c 2
b 0
f 1
nc 72
nop 0
dl 0
loc 100
rs 5.9166

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 Icinga\Module\TrapDirector\Tables;
4
5
use Icinga\Web\Request;
6
use Icinga\Web\Url;
7
use Icinga\Web\Widget;
8
use Icinga\Web\Widget\Paginator;
9
10
use Icinga\Module\Trapdirector\Tables\TrapTable;
11
12
13
14
class HandlerTableList extends TrapTable
15
{	
16
	// Db connection : getConnection / setConnection
17
	protected $connection;
18
	
19
	// translate
20
	protected $doTranslate=false;
21
	protected $MIB;
22
	
23
	// status
24
	protected $status_display=array(
25
		-2	=>'ignore',
26
		-1 => '-',
27
		0	=> 'OK',
28
		1	=> 'warning',
29
		2	=> 'critical',
30
		3	=> 'unknown',);
31
	
32
	// Filters 
33
	
34
    protected $filter;
35
    protected $enforcedFilters = array();
36
    protected $searchColumns = array();
37
	
38
	protected function getTitles() {
39
		return $this->moduleConfig->getHandlerListTitles();
40
	}
41
	
42
	public function setMibloader($mibloader)
43
	{
44
		$this->MIB=$mibloader;
45
		$this->doTranslate=true;
46
	}
47
	// ******************  Render table in html  
48
    public function __toString()
49
    {
50
        return $this->render();
51
    }
52
	
53
	public function render()
54
	{
55
		$data=$this->getTable();
56
		$view = $this->getView();
57
		$this->columnCount = count($this->getTitles());
58
		$this->lastDay=null;
59
		// Table start
60
		$htm  = '<table class="simple common-table table-row-selectable">';
61
		
62
		// Titles
63
		$htm .= "<thead>\n  <tr>\n";
64
		$titles = $this->getTitles();
65
		foreach ($titles as $title) 
66
		{
67
			$htm .= '    <th>' . $view->escape($view->translate($title)) . "</th>\n";
68
		}
69
		$htm .= "  </tr>\n</thead>\n";
70
		
71
		// Rows
72
		$htm .= "<tbody>\n";
73
		
74
		foreach ($data as $row) 
75
		{
76
			$firstCol = true;
77
			// Put date header
78
			//$htm .= $this->renderDayIfNew($row->timestamp);
79
			
80
			
81
			// Render row
82
			$htm .= '<tr '.' >';
83
			foreach ( $titles as $rowkey => $title) 
84
			{
85
				// Check missing value
86
				if (property_exists($row, $rowkey)) 
87
				{
88
					switch ($rowkey)
89
					{
90
						case 'action_match': // display text levels
91
						case 'action_nomatch':
92
							$val=$this->status_display[$row->$rowkey];
93
						break;
94
						case 'trap_oid': // try to traslate oids.
95
						
96
							if ($this->doTranslate===true)
97
							{
98
								$oidName=$this->MIB->translateOID($row->$rowkey);
99
								if (isset($oidName['name']))
100
								{
101
									$val=$oidName['name'];
102
								}
103
								else
104
								{
105
									$val = $row->$rowkey;
106
								}								
107
							}
108
							else
109
							{
110
								$val = $row->$rowkey;
111
							}
112
						break;
113
						case 'host_name': // switch to hostgroup if name is null
114
							if ($row->$rowkey == null)
115
							{
116
								$val = $row->host_group_name;
117
							}
118
							else
119
							{
120
								$val = $row->$rowkey;
121
							}
122
						break;
123
						default:
124
							$val = $row->$rowkey;
125
					}
126
					if ($rowkey == 'trap_oid' && $this->doTranslate===true)
127
					{					
128
							
129
					}
130
				} else {
131
					$val = '-';
132
				}
133
				if ($firstCol === true) { // Put link in first column for trap detail.
134
					$htm .= '<td>' 
135
							. $view->qlink(
136
									$view->escape($val),  
137
									Url::fromPath(
138
										$this->moduleConfig->urlPath() . '/handler/add', 
139
										array('ruleid' => $row->id)
140
									)
141
							)
142
							. '</td>';
143
				} else {
144
					$htm .= '<td>' . $view->escape($val) . '</td>';
145
				}
146
				$firstCol=false;
147
			}
148
			$htm .= "<tr>\n";
149
		}
150
		$htm .= "</tbody></table>\n";
151
		//$htm .= "Filter : " . $this->filter."<br>\n";
152
		return $htm;
153
154
	}
155
156
    public function count()
157
    {
158
        //$db = $this->connection()->getConnection();
159
        $query = clone($this->getBaseQuery());
160
        $query->reset('order')->columns(array('COUNT(*)'));
161
        $this->applyFiltersToQuery($query);
162
163
		$db=$this->db();
164
		
165
		$query = $db->select()->from(
166
            $this->moduleConfig->getTrapRuleName(),
167
            array('COUNT(*)')
168
        );
169
		
170
        return $db->fetchOne($query);
171
    }
172
	
173
    public function getPaginator()
174
    {
175
        $paginator = new Paginator();
176
        $paginator->setQuery($this);
177
178
        return $paginator;
179
    }
180
	
181
	// ****************** DB connection and query
182
	
183
	protected function getTable()
184
	{
185
		$db=$this->db();
186
		
187
		$query = $this->getBaseQuery();
188
		
189
       if ($this->hasLimit() || $this->hasOffset()) {
190
            $query->limit($this->getLimit(), $this->getOffset());
191
        }		
192
		
193
		return $db->fetchAll($query);
194
	}
195
	 
196
    public function getBaseQuery()
197
    {
198
		$db=$this->db();
199
		
200
		$query = $db->select()->from(
201
            $this->moduleConfig->getTrapRuleName(),
202
            $this->moduleConfig->getHandlerListDisplayColumns()
203
        )->order('host_name DESC,trap_oid DESC');
204
205
        return $query;
206
    }	 
207
	
208
	// ****************** Filters
209
210
    protected function getSearchColumns()
211
    {
212
        return $this->getColumns();
213
    }
214
	
215
	public function getColumns()
216
	{
217
		return $this->moduleConfig->getHandlerListDisplayColumns();
218
	}
219
220
    public function setFilter($filter)
221
    {
222
        $this->filter = $filter;
223
        return $this;
224
    }
225
	
226
	public function getFilterEditor(Request $request)
227
    {
228
        $filterEditor = Widget::create('filterEditor')
229
            ->setColumns(array_keys($this->getColumns()))
230
            ->setSearchColumns(array_keys($this->getSearchColumns()))
231
            ->preserveParams('limit', 'sort', 'dir', 'view', 'backend')
232
            ->ignoreParams('page')
233
            ->handleRequest($request);
234
235
        $filter = $filterEditor->getFilter();
236
        $this->setFilter($filter);
237
238
        return $filterEditor;
239
    }
240
	
241
    protected function applyFiltersToQuery($query)
242
    {
243
		// TODO : implement
244
		
245
        /*$filter = null;
246
        $enforced = $this->enforcedFilters;
247
        if ($this->filter && ! $this->filter->isEmpty()) {
248
            $filter = $this->filter;
249
        } elseif (! empty($enforced)) {
250
            $filter = array_shift($enforced);
251
        }
252
        if ($filter) {
253
            foreach ($enforced as $f) {
254
                $filter->andFilter($f);
255
            }
256
            $query->where($this->renderFilter($filter));
257
        }
258
		*/
259
260
        return $query;
261
    }	
262
263
}
264