Completed
Push — 2.0 ( ada449...a2425b )
by Vermeulen
01:55
created

ConnectDB::connectToDatabase()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 28
rs 9.472
c 0
b 0
f 0
cc 4
nc 3
nop 1
1
<?php
2
3
namespace BfwSql\Runners;
4
5
/**
6
 * Connect to all declared databases
7
 * It's a runner, so is called when the module is initialized.
8
 * 
9
 * @package bfw-sql
10
 * @author Vermeulen Maxime <[email protected]>
11
 * @version 2.0
12
 */
13
class ConnectDB extends AbstractRunner
14
{
15
    /**
16
     * @const ERR_NO_CONNECTION_KEYNAME Exception code if the connection has no
17
     * keyName and there are many connection declared.
18
     */
19
    const ERR_NO_CONNECTION_KEYNAME = 2502001;
20
    
21
    /**
22
     * @const ERR_NO_BASE_TYPE Exception code if the base type is not declared.
23
     */
24
    const ERR_NO_BASE_TYPE = 2502002;
25
    
26
    /**
27
     * {@inheritdoc}
28
     * Run the system to connect to all database declared.
29
     */
30
    public function run()
31
    {
32
        $moduleConfig    = $this->module->getConfig();
33
        $configListBases = $moduleConfig->getValue('bases', 'bases.php');
34
        $configNbBases   = count($configListBases);
0 ignored issues
show
Unused Code introduced by
$configNbBases is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
35
        
36
        $this->module->listBases = [];
37
38
        foreach ($configListBases as $baseInfos) {
39
            $this->connectToDatabase($baseInfos);
40
        }
41
    }
42
    
43
    /**
44
     * Create the connection to a database
45
     * 
46
     * @param \stdClass $baseInfos Information about the database to connect
47
     * 
48
     * @throws \Exception
49
     * 
50
     * @return void
51
     */
52
    protected function connectToDatabase($baseInfos)
53
    {
54
        if (empty($baseInfos->baseType)) {
55
            throw new \Exception(
56
                'bfw-sql : All connection should have a type declared.',
57
                self::ERR_NO_BASE_TYPE
58
            );
59
        }
60
61
        $baseKey         = $baseInfos->baseKeyName;
62
        $moduleConfig    = $this->module->getConfig();
63
        $configListBases = $moduleConfig->getValue('bases', 'bases.php');
64
        $configNbBases   = count($configListBases);
65
66
        if (empty($baseKey) && $configNbBases > 1) {
67
            throw new \Exception(
68
                'bfw-sql : They are multiple connection defined,'
69
                .' a keyName must be define to each connection.',
70
                self::ERR_NO_CONNECTION_KEYNAME
71
            );
72
        }
73
74
        $usedClass        = \BfwSql\UsedClass::getInstance();
75
        $connectClassName = $usedClass->obtainClassNameToUse('SqlConnect');
76
        
77
        $this->module->listBases[$baseKey] = new $connectClassName($baseInfos);
78
        $this->module->listBases[$baseKey]->createConnection();
79
    }
80
}
81