Passed
Push — master ( df2595...b8f383 )
by Patrick
01:54
created

ReceivedController::trapdetailAction()   C

Complexity

Conditions 11
Paths 150

Size

Total Lines 93
Code Lines 53

Duplication

Lines 0
Ratio 0 %

Importance

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