Passed
Push — master ( 3d7e39...d44cfc )
by Patrick
02:50
created

TrapDBQuery::deleteTrap()   A

Complexity

Conditions 6
Paths 13

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 13
c 1
b 0
f 0
nc 13
nop 2
dl 0
loc 22
rs 9.2222
1
<?php
2
3
namespace Icinga\Module\Trapdirector\TrapsActions;
4
5
use Exception;
6
use Zend_Db_Expr;
7
8
/**
9
 * Database queries for UI (on Trap database)
10
 *
11
 * @license GPL
12
 * @author Patrick Proy
13
 * @package trapdirector
14
 * @subpackage UI
15
 *
16
 */
17
abstract class TrapDBQuery
18
{
19
20
21
    abstract protected function getTrapCtrl();
22
    abstract public function getDbConn();
23
    
24
    /** Add handler rule in traps DB
25
     *	@param array $params : array(<db item>=><value>)
26
     *	@return int inserted id
27
     */
28
    public function addHandlerRule($params)
29
    {
30
        // TODO Check for rule consistency
31
        
32
        $dbConn = $this->getDbConn();
33
        if ($dbConn === null) throw new \ErrorException('uncatched db error');
34
        // Add last modified date = creation date and username
35
        $params['created'] = new Zend_Db_Expr('NOW()');
36
        $params['modified'] = new 	Zend_Db_Expr('NOW()');
37
        $params['modifier'] = $this->Auth()->getUser()->getUsername();
0 ignored issues
show
Bug introduced by
The method Auth() does not exist on Icinga\Module\Trapdirect...rapsActions\TrapDBQuery. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

37
        $params['modifier'] = $this->/** @scrutinizer ignore-call */ Auth()->getUser()->getUsername();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
38
        
39
        $query=$dbConn->insert(
40
            $this->getTrapCtrl()->getModuleConfig()->getTrapRuleName(),
41
            $params
42
            );
43
        if($query==false)
44
        {
45
            return null;
46
        }
47
        return $dbConn->lastInsertId();
48
    }
49
    
50
    /** Update handler rule in traps DB
51
     *	@param array $params : (<db item>=><value>)
52
     *   @param integer $ruleID : rule id in db
53
     *	@return array affected rows
54
     */
55
    public function updateHandlerRule($params,$ruleID)
56
    {
57
        // TODO Check for rule consistency
58
        $dbConn = $this->getDbConn();
59
        if ($dbConn === null) throw new \ErrorException('uncatched db error');
60
        // Add last modified date = creation date and username
61
        $params['modified'] = new 	Zend_Db_Expr('NOW()');
62
        $params['modifier'] = $this->getTrapCtrl()->Auth()->getUser()->getUsername();
63
        
64
        $numRows=$dbConn->update(
65
            $this->getTrapCtrl()->getModuleConfig()->getTrapRuleName(),
66
            $params,
67
            'id='.$ruleID
68
            );
69
        return $numRows;
70
    }
71
    
72
    /** Delete rule by id
73
     *	@param int $ruleID rule id
74
     */
75
    public function deleteRule($ruleID)
76
    {
77
        if (!preg_match('/^[0-9]+$/',$ruleID)) { throw new Exception('Invalid id');  }
78
        
79
        $dbConn = $this->getDbConn();
80
        if ($dbConn === null) throw new \ErrorException('uncatched db error');
81
        
82
        $query=$dbConn->delete(
83
            $this->getTrapCtrl()->getModuleConfig()->getTrapRuleName(),
84
            'id='.$ruleID
85
            );
86
        return $query;
87
    }
88
    
89
    /** Delete trap by ip & oid
90
     *	@param $ip string source IP (v4 or v6)
91
     *	@param $oid string oid
92
     */
93
    public function deleteTrap($ip,$oid)
94
    {
95
        
96
        $dbConn = $this->getDbConn();
97
        if ($dbConn === null) throw new \ErrorException('uncatched db error');
98
        $condition=null;
99
        if ($ip != null)
100
        {
101
            $condition="source_ip='$ip'";
102
        }
103
        if ($oid != null)
104
        {
105
            $condition=($condition===null)?'':$condition.' AND ';
106
            $condition.="trap_oid='$oid'";
107
        }
108
        if($condition === null) return null;
109
        $query=$dbConn->delete(
110
            $this->getTrapCtrl()->getModuleConfig()->getTrapTableName(),
111
            $condition
112
            );
113
        // TODO test ret code etc...
114
        return $query;
115
    }
116
    
117
    
118
    /** count trap by ip & oid
119
     *	@param $ip string source IP (v4 or v6)
120
     *	@param $oid string oid
121
     */
122
    public function countTrap($ip,$oid)
123
    {
124
        
125
        $dbConn = $this->getDbConn();
126
        if ($dbConn === null) throw new \ErrorException('uncatched db error');
127
        
128
        $condition=null;
129
        if ($ip != null)
130
        {
131
            $condition="source_ip='$ip'";
132
        }
133
        if ($oid != null)
134
        {
135
            $condition=($condition===null)?'':$condition.' AND ';
136
            $condition.="trap_oid='$oid'";
137
        }
138
        if($condition === null) return 0;
139
        $query=$dbConn->select()
140
        ->from(
141
            $this->getTrapCtrl()->getModuleConfig()->getTrapTableName(),
142
            array('num'=>'count(*)'))
143
            ->where($condition);
144
            $return_row=$dbConn->fetchRow($query);
145
            return $return_row->num;
146
    }
147
    
148
    /** get configuration value
149
     *	@param string $element : configuration name in db
150
     */
151
    public function getDBConfigValue($element)
152
    {
153
        
154
        $dbConn = $this->getDbConn();
155
        if ($dbConn === null) throw new \ErrorException('uncatched db error');
156
        
157
        $query=$dbConn->select()
158
        ->from(
159
            $this->getTrapCtrl()->getModuleConfig()->getDbConfigTableName(),
160
            array('value'=>'value'))
161
            ->where('name=?',$element);
162
            $return_row=$dbConn->fetchRow($query);
163
            if ($return_row==null)  // value does not exists
164
            {
165
                $default=$this->getTrapCtrl()->getModuleConfig()->getDBConfigDefaults();
166
                if ( ! isset($default[$element])) return null; // no default and not value
167
                
168
                $this->addDBConfigValue($element,$default[$element]);
169
                return $default[$element];
170
            }
171
            if ($return_row->value == null) // value id empty
172
            {
173
                $default=$this->getTrapCtrl()->getModuleConfig()->getDBConfigDefaults();
174
                if ( ! isset($default[$element])) return null; // no default and not value
175
                $this->setDBConfigValue($element,$default[$element]);
176
                return $default[$element];
177
            }
178
            return $return_row->value;
179
    }
180
    
181
    /** add configuration value
182
     *	@param string $element : name of config element
183
     *   @param string $value : value
184
     */
185
    
186
    public function addDBConfigValue($element,$value)
187
    {
188
        
189
        $dbConn = $this->getDbConn();
190
        if ($dbConn === null) throw new \ErrorException('uncatched db error');
191
        
192
        $query=$dbConn->insert(
193
            $this->getTrapCtrl()->getModuleConfig()->getDbConfigTableName(),
194
            array(
195
                'name' => $element,
196
                'value'=>$value
197
            )
198
            );
199
        return $query;
200
    }
201
    
202
    /** set configuration value
203
     *	@param string $element : name of config element
204
     *   @param string $value : value
205
     */
206
    public function setDBConfigValue($element,$value)
207
    {
208
        
209
        $dbConn = $this->getDbConn();
210
        if ($dbConn === null) throw new \ErrorException('uncatched db error');
211
        
212
        $query=$dbConn->update(
213
            $this->getTrapCtrl()->getModuleConfig()->getDbConfigTableName(),
214
            array('value'=>$value),
215
            'name=\''.$element.'\''
216
            );
217
        return $query;
218
    }
219
    
220
    
221
}