ReceivedController::trapdetailAction()   C
last analyzed

Complexity

Conditions 12
Paths 151

Size

Total Lines 94
Code Lines 54

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 54
c 1
b 0
f 0
nc 151
nop 0
dl 0
loc 94
rs 6.5416

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\Controllers;
4
5
use Icinga\Web\Url;
6
7
use Exception;
8
9
use Icinga\Module\Trapdirector\TrapsController;
10
11
/** 
12
*/
13
class ReceivedController extends TrapsController
14
{
15
	/**
16
	 * List traps by date
17
	 */
18
	public function indexAction()
19
	{	
20
		$this->checkReadPermission();
21
		$this->prepareTabs()->activate('traps');
22
23
		$dbConn = $this->getUIDatabase()->getDb();
24
		if ($dbConn === null) throw new \ErrorException('uncatched db error');
25
		$this->getTrapListTable()->setConnection($dbConn);
26
		
27
		// Apply pagination limits
28
		$this->view->table=$this->applyPaginationLimits($this->getTrapListTable(),$this->getModuleConfig()->itemListDisplay());		
29
		
30
		// Set Filter
31
		//$postData=$this->getRequest()->getPost();
32
		$filter=array();
33
		$filter['q']=$this->params->get('q');//(isset($postData['q']))?$postData['q']:'';
34
		$filter['done']=$this->params->get('done');
35
		$this->view->filter=$filter;
36
		$this->view->table->updateFilter(Url::fromRequest(),$filter);
37
		
38
		//$this->view->filterEditor = $this->getTrapListTable()->getFilterEditor($this->getRequest());
39
40
	}
41
42
	/** 
43
	*	Trap detail page
44
	*/	
45
	public function trapdetailAction() 
46
	{
47
		
48
		$this->checkReadPermission();
49
		// set up tab
50
		$this->getTabs()->add('get',array(
51
			'active'	=> true,
52
			'label'		=> $this->translate('Detailed status'),
53
			'url'		=> Url::fromRequest()
54
		));
55
		// get id
56
		$trapid=$this->params->get('id');
57
		$this->view->trapid=$trapid;
58
		$queryArray=$this->getModuleConfig()->trapDetailQuery();
59
		
60
		$dbConn = $this->getUIDatabase()->getDbConn();
61
		if ($dbConn === null) throw new \ErrorException('uncatched db error');
62
		
63
		// URL to add a handler
64
		$this->view->addHandlerUrl=Url::fromPath(
65
			$this->getModuleConfig()->urlPath() . '/handler/add',
66
			array('fromid' => $trapid));
67
		// ***************  Get main data
68
		// extract columns and titles;
69
		$elmts=NULL;
70
		foreach ($queryArray as $key => $val) {
71
			$elmts[$key]=$val[1];
72
		}
73
		
74
		// Do DB query for trap. 
75
		try
76
		{
77
		    $query = $dbConn->select()
78
				->from($this->moduleConfig->getTrapTableName(),$elmts)
79
				->where('id=?',$trapid);
80
				$trapDetail=$dbConn->fetchRow($query);
81
			if ( $trapDetail == null) throw new Exception('No traps was found with id = '.$trapid);
82
		}
83
		catch (Exception $e)
84
		{
85
			$this->displayExitError('Trap detail',$e->getMessage());
86
			return;
87
		}
88
89
		// Store result in array (with Titles).
90
		foreach ($queryArray as $key => $val) {
91
			if ($key == 'timestamp') {
92
				$cval=strftime('%c',$trapDetail->$key);
93
			} else {
94
				$cval=$trapDetail->$key;
95
			}
96
			array_push($queryArray[$key],$cval);
97
		}
98
		$this->view->rowset = $queryArray;
99
100
		// **************   Check for additionnal data
101
		
102
		// extract columns and titles;
103
		$queryArrayData=$this->getModuleConfig()->trapDataDetailQuery();
104
		$data_elmts=NULL;
105
		foreach ($queryArrayData as $key => $val) {
106
			$data_elmts[$key]=$val[1];
107
		}
108
		try
109
		{		
110
		    $query = $dbConn->select()
111
				->from($this->moduleConfig->getTrapDataTableName(),$data_elmts)
112
				->where('trap_id=?',$trapid);
113
			$trapDetail=$dbConn->fetchAll($query);
114
		}
115
		catch (Exception $e)
116
		{
117
			$this->displayExitError('Trap detail',$e->getMessage());
118
		}
119
		// TODO : code this in a better & simpler way
120
		if ($trapDetail == null ) 
121
		{
122
			$this->view->data=false;
123
		}
124
		else
125
		{
126
			$this->view->data=true;
127
			// Store result in array.
128
			$trapval=array();
129
			foreach ($trapDetail as $key => $val) 
130
			{	
131
				$trapval[$key]=array();
132
				foreach (array_keys($queryArrayData) as $vkey ) 
133
				{
134
					array_push($trapval[$key],$val->$vkey);
135
				}
136
			}
137
			$this->view->data_val=$trapval;
138
			$this->view->data_title=$queryArrayData;
139
		}
140
	}
141
142
	/**
143
	 * List traps by hosts
144
	 */
145
	public function hostsAction()
146
	{
147
	    $this->checkReadPermission();
148
	    $this->prepareTabs()->activate('hosts');
149
	    
150
	    $dbConn = $this->getUIDatabase()->getDb();
151
	    if ($dbConn === null) throw new \ErrorException('uncatched db error');
152
	    
153
	    $this->getTrapHostListTable()->setConnection($dbConn);
154
	    
155
	    // Apply pagination limits
156
	    $this->view->table=$this->applyPaginationLimits($this->getTrapHostListTable(),$this->getModuleConfig()->itemListDisplay());
157
	    
158
	    // Set Filter
159
	    //$postData=$this->getRequest()->getPost();
160
	    $filter=array();
161
	    $filter['q']=$this->params->get('q');//(isset($postData['q']))?$postData['q']:'';
162
	    $filter['done']=$this->params->get('done');
163
	    $this->view->filter=$filter;
164
	    $this->view->table->updateFilter(Url::fromRequest(),$filter);
165
	}
166
	
167
	public function deleteAction()
168
	{
169
		$this->checkConfigPermission();
170
		$this->prepareTabs()->activate('delete');
171
				
172
		return;
173
	}
174
	
175
	/**
176
	 * Create tabs for /received
177
	 * @return object tabs
178
	 */
179
	protected function prepareTabs()
180
	{
181
		return $this->getTabs()->add('traps', array(
182
			'label'	=> $this->translate('Traps'),
183
			'url'   => $this->getModuleConfig()->urlPath() . '/received')
184
		    )
185
		    ->add('hosts', array(
186
		        'label' => $this->translate('Hosts'),
187
		        'url'   => $this->getModuleConfig()->urlPath() . '/received/hosts')
188
		    )
189
		    ->add('delete', array(
190
			'label' => $this->translate('Delete'),
191
			'url'   => $this->getModuleConfig()->urlPath() . '/received/delete')
192
		  );
193
	} 
194
195
	/**
196
	 * Helper to count / delete lines
197
	 * POST params : 
198
	 * - OID : partial oid of traps (if empty not used in filter)
199
	 * - IP : IP or partial IP of host (if empty not used in filter)
200
	 * - action : 
201
	 *     count : return JSON : status:OK, count : number of lines selected by filter
202
	 *     delete : delete traps selected by filter
203
	 */
204
	public function deletelinesAction()
205
	{
206
		$postData=$this->getRequest()->getPost();
207
		if (isset($postData['IP']) && isset($postData['OID']) && isset($postData['action']))
208
		{
209
			$ip=$postData['IP'];
210
			$oid=$postData['OID'];
211
			$action=$postData['action'];
212
		}
213
		else
214
		{
215
			$this->_helper->json(array('status'=>'Missing variables'));
216
			return;
217
		}
218
		if ($action =="count")
219
		{
220
			$this->_helper->json(array('status'=>'OK','count'=>$this->getUIDatabase()->countTrap($ip,$oid)));
221
			return;
222
		}
223
		if ($action =="delete")
224
		{
225
		    $this->_helper->json(array('status'=>'OK','count'=>$this->getUIDatabase()->deleteTrap($ip,$oid)));
226
			return;
227
		}		
228
		$this->_helper->json(array('status'=>'unknown action'));
229
	}
230
	
231
}