Passed
Push — master ( f89732...ddbf74 )
by Patrick
02:13
created

MibDatabase::update_oid_update()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 44
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 30
nc 4
nop 0
dl 0
loc 44
rs 8.8177
c 1
b 0
f 0
1
<?php
2
3
4
5
trait MibDatabase
6
{
7
    /** @return \Trapdirector\Logging */
8
    abstract public function getLogging();
9
    
10
    /** @return \Trapdirector\Database */
11
    abstract public function getTrapsDB();
12
  
13
    /**
14
     * Update object in DB with object in dbOidIndex if name/mib/type has changed.
15
     * @return number : 0=unchanged, 1 = changed, 2=created
16
     */
17
    private function update_oid_update()
18
    {
19
        
20
        $db_conn=$this->getTrapsDB()->db_connect_trap();
21
        
22
        if ($this->dbOidIndex[$this->oidDesc['oid']]['key'] == -1)
23
        { // newly created.
24
            return 0;
25
        }
26
        $oidIndex=$this->dbOidIndex[$this->oidDesc['oid']]['key']; // Get index in dbOidAll
27
        $dbOid=$this->dbOidAll[$oidIndex]; // Get array of element
28
        if ( $this->oidDesc['name'] != $dbOid['name'] ||
29
            $this->oidDesc['mib'] != $dbOid['mib'] ||
30
            $this->oidDesc['type'] !=$dbOid['type']
31
            )
32
        { // Do update
33
            $sql='UPDATE '.$this->getTrapsDB()->dbPrefix.'mib_cache SET '.
34
                'name = :name , type = :type , mib = :mib , textual_convention = :tc , display_hint = :display_hint'.
35
                ', syntax = :syntax, type_enum = :type_enum, description = :description '.
36
                ' WHERE id= :id';
37
            $sqlQuery=$db_conn->prepare($sql);
38
            
39
            $sqlParam=array(
40
                ':name' => $this->oidDesc['name'],
41
                ':type' => $this->oidDesc['type'],
42
                ':mib' => $this->oidDesc['mib'],
43
                ':tc' =>  $this->oidDesc['textconv']??'null',
44
                ':display_hint' => $this->oidDesc['dispHint']??'null' ,
45
                ':syntax' => $this->oidDesc['syntax']==null??'null',
46
                ':type_enum' => $this->oidDesc['type_enum']??'null',
47
                ':description' => $this->oidDesc['description']??'null',
48
                ':id' => $this->dbOidAll[$this->dbOidIndex[$this->oidDesc['oid']]['id']]
49
            );
50
            
51
            if ($sqlQuery->execute($sqlParam) === false) {
52
                $this->getLogging()->log('Error in query : ' . $sql,ERROR,'');
53
            }
54
            $this->getLogging()->log('Trap updated : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],DEBUG );
55
            return 1;
56
        }
57
        else
58
        {
59
            $this->getLogging()->log('Trap unchanged : '.$this->oidDesc['name'] . ' / OID : '.$this->oidDesc['oid'],DEBUG );
60
            return 0;
61
        }
62
    }
63
    
64
}