Passed
Push — master ( 95b1cb...993560 )
by Patrick
01:56
created

UIDatabase::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
3
use Icinga\Module\Trapdirector\TrapsController;
4
use Icinga\Data\Db\DbConnection as IcingaDbConnection;
5
6
/**
7
 * Database functions for user interface
8
 * 
9
 * @license GPL
10
 * @author Patrick Proy
11
 * @package trapdirector
12
 * @subpackage UI
13
 *
14
 */
15
class UIDatabase
16
{
17
    
18
    /** @var TrapsController $trapController TrapController 'parent' class */
19
    private  $trapController;
20
    
21
    /** @var Zend_Db_Adapter_Abstract $trapDB Trap Database*/
1 ignored issue
show
Bug introduced by
The type Zend_Db_Adapter_Abstract was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
22
    private $trapDB;
23
 
24
    /** @var Zend_Db_Adapter_Abstract $trapDB Icinga IDO database*/
25
    private $idoDB;
26
    
27
    /**
28
     * 
29
     * @param TrapsController $trapCtrl
30
     */
31
    function __construct(TrapsController $trapCtrl)
32
    {
33
        $this->trapController=$trapCtrl;
34
    }
35
    
36
    /**
37
     * Test if database version >= min database version
38
     * 
39
     * @param \Zend_Db_Adapter_Abstract $dbConn
40
     * @param int $min Minimum version
41
     * @param bool $test Test mode
42
     * @param string $DBname Name of DB
43
     * @return NULL|array 
44
     */
45
    protected function testDbVersion($dbAdapter,int $min,bool $test, string $DBname)
46
    {
47
        try
48
        {
49
            $query = $dbAdapter->select()
50
            ->from($this->trapController->getModuleConfig()->getDbConfigTableName(),'value')
51
            ->where('name=\'db_version\'');
52
            $version=$dbAdapter->fetchRow($query);
53
            if ( ($version == null) || ! property_exists($version,'value') )
54
            {
55
                if ($test) return array(4,$DBname);
56
                $this->trapController->redirectNow('trapdirector/settings?dberror=4');
57
                return null;
58
            }
59
            if ($version->value < $min)
60
            {
61
                if ($test) return array(5,$version->value,$min);
62
                $this->trapController->redirectNow('trapdirector/settings?dberror=5');
63
                return null;
64
            }
65
        }
66
        catch (Exception $e)
67
        {
68
            if ($test) return array(3,$DBname,$e->getMessage());
69
            $this->trapController->redirectNow('trapdirector/settings?dberror=4');
70
        }
71
        return null;
72
    }
73
    
74
    /**	Get Database connexion
75
     *	@param $DBname string DB name in resource.ini_ge
76
     *	@param $test bool if set to true, returns error code and not database
77
     *	@param $test_version bool if set to flase, does not test database version of trapDB
78
     *	@return Zend_Db_Adapter_Abstract|array|null : if test=false, returns DB connexion, else array(error_num,message) or null on error.
79
     */
80
    protected function getDbByName($DBname,$test=false,$test_version=true)
81
    {
82
        try
83
        {
84
            $dbconn = IcingaDbConnection::fromResourceName($DBname);
85
        }
86
        catch (Exception $e)
87
        {
88
            if ($test) return array(2,$DBname);
89
            $this->trapController->redirectNow('trapdirector/settings?dberror=2');
90
            return null;
91
        }
92
93
        try
94
        {
95
            $dbAdapter=$dbconn->getDbAdapter();
96
            
97
        }
98
        catch (Exception $e)
99
        {
100
            if ($test) return array(3,$DBname,$e->getMessage());
101
            $this->trapController->redirectNow('trapdirector/settings?dberror=3');
102
            return null;
103
        }
104
        
105
        if ($test_version == true) {
106
            $testRet=$this->testDbVersion($dbAdapter, $this->trapController->getModuleConfig()->getDbMinVersion(), $test, $DBname);
107
            if ($testRet != null) 
108
            {
109
                return $testRet;
110
            }
111
        }
112
        if ($test) return array(0,'');
113
        return $dbAdapter;
114
    }
115
116
    /**
117
     * Get Trap database
118
     * @param boolean $test
119
     * @return Zend_Db_Adapter_Abstract|array|null : if test=false, returns DB connexion, else array(error_num,message) or null on error.
120
     */
121
    public function getDb($test=false)
122
    {
123
        if ($this->trapDB != null && $test = false) return $this->trapDB;
124
        
125
        $dbresource=$this->trapController->Config()->get('config', 'database');
126
        
127
        if ( ! $dbresource )
128
        {
129
            if ($test) return array(1,'');
130
            $this->redirectNow('trapdirector/settings?dberror=1');
0 ignored issues
show
Bug introduced by
The method redirectNow() does not exist on UIDatabase. ( Ignorable by Annotation )

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

130
            $this->/** @scrutinizer ignore-call */ 
131
                   redirectNow('trapdirector/settings?dberror=1');

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...
131
            return null;
132
        }
133
        $retDB=$this->getDbByName($dbresource,$test,true);
134
        
135
        if ($test === true) return $retDB;
136
        
137
        $this->trapDB=$retDB;
0 ignored issues
show
Documentation Bug introduced by
It seems like $retDB can also be of type array. However, the property $trapDB is declared as type Zend_Db_Adapter_Abstract. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
138
        return $this->trapDB;
139
    }
140
    
141
    /**
142
     * Get IDO Database 
143
     * @param boolean $test
144
     * @return Zend_Db_Adapter_Abstract|array]|NULL if test=false, returns DB connexion, else array(error_num,message) or null on error.
145
     */
146
    public function getIdoDb($test=false)
147
    {
148
        if ($this->idoDB != null && $test = false) return $this->idoDB;
149
        // TODO : get ido database directly from icingaweb2 config -> (or not if using only API)
150
        $dbresource=$this->Config()->get('config', 'IDOdatabase');;
0 ignored issues
show
Bug introduced by
The method Config() does not exist on UIDatabase. ( Ignorable by Annotation )

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

150
        $dbresource=$this->/** @scrutinizer ignore-call */ Config()->get('config', 'IDOdatabase');;

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...
151
        
152
        if ( ! $dbresource )
153
        {
154
            if ($test) return array(1,'No database in config.ini');
155
            $this->redirectNow('trapdirector/settings?idodberror=1');
156
            return null;
157
        }
158
        
159
        try
160
        {
161
            $dbconn = IcingaDbConnection::fromResourceName($dbresource);
162
        }
163
        catch (Exception $e)
164
        {
165
            if ($test) return array(2,"Database $dbresource does not exists in IcingaWeb2");
166
            $this->redirectNow('trapdirector/settings?idodberror=2');
167
            return null;
168
        }
169
        
170
        if ($test == false)
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
171
        {
172
            $this->idoDB = $dbconn->getDbAdapter();
173
            return $this->idoDB;
174
        }
175
        
176
        try
177
        {
178
            $query = $dbconn->select()
179
            ->from('icinga_dbversion',array('version'));
180
            $version=$dbconn->fetchRow($query);
181
            if ( ($version == null) || ! property_exists($version,'version') )
182
            {
183
                return array(4,"$dbresource does not look like an IDO database");
184
            }
185
        }
186
        catch (Exception $e)
187
        {
188
            return array(3,"Error connecting to $dbresource : " . $e->getMessage());
189
        }
190
        
191
        return array(0,'');
192
    }
193
    
194
}