Completed
Pull Request — master (#15)
by Christopher
53:32 queued 38:32
created

Connection   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 119
Duplicated Lines 18.49 %

Coupling/Cohesion

Components 2
Dependencies 1

Importance

Changes 12
Bugs 2 Features 3
Metric Value
wmc 26
c 12
b 2
f 3
lcom 2
cbo 1
dl 22
loc 119
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
C __construct() 0 28 8
A db() 0 4 1
A close() 0 4 1
B checkParam() 0 13 7
A connectMySQL() 0 12 2
A connectSQLite() 0 10 2
A connectPostrges() 0 4 1
A connectMSSQL() 11 11 2
A connectSQL() 11 11 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace Ganga\Potato;
4
5
use PDO;
6
/**
7
 * Class to define connection to sqlite database;
8
 */
9
class Connection
10
{
11
    protected static $conn;
12
    public static $dbType;
13
    public static $dbName;
14
15
    /**
16
     * Constructor
17
     * Declare an instance of SQLite class
18
     */
19
    public function __construct($config)
20
    {
21
        if (!is_array($config)) {
22
            throw new PotatoException("Database Config must be an array");
23
        }
24
25
        if (!isset($config['type']) || empty($config['type'])) {
26
            throw new PotatoException("You must specify a database type in the config");
27
        }
28
29
        if (!isset($config['database']) || empty($config['database'])) {
30
            throw new PotatoException("You must provide a database in the config");
31
        }
32
33
        self::$dbName = $config['database'];
34
        self::$dbType = strtolower($config['type']);
35
36
        switch (self::$dbType) {
37
            case 'sqlite':
38
                self::$conn = $this->connectSQLite($config);
39
                break;
40
            case 'mysql':
41
                self::$conn = $this->connectMySQL($config);
42
                break;
43
            default:
44
                self::$conn = $this->connectSQL($config);
45
        }
46
    }
47
48
    /**
49
     * Get an instance of the db connection
50
     * @return SQLite3 connection
51
     */
52
    public static function db()
53
    {
54
        return self::$conn;
55
    }
56
     public static function close()
57
     {
58
        self::$conn = null;
59
     }
60
61
    public function checkParam($config) {
62
        if (!isset($config['host']) || empty($config['host'])) {
63
            throw new PotatoException("You must provide a host to connect to database");
64
        }
65
66
        if (!isset($config['user']) || empty($config['user'])) {
67
            throw new PotatoException("You must provide a user for database");
68
        }
69
70
        if (!isset($config['password']) || empty($config['password'])) {
71
            throw new PotatoException("You must provide a password for  database");
72
        }
73
    }
74
75
    public function connectMySQL($config)
76
    {
77
        $this->checkParam($config);
78
79
        try {
80
            $db = new PDO("mysql:host=".$config['host'].";dbname=".$config['database'], $config['user'], $config['password']);
81
        } catch (PDOException $e) {
0 ignored issues
show
Bug introduced by
The class Ganga\Potato\PDOException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
82
            throw new PotatoException($e->getMessage());
83
        }
84
85
        return $db;
86
    }
87
88
    public function connectSQLite($config)
89
    {
90
        try {
91
            $db =  new PDO("sqlite:".$config['database']);
92
        } catch (PDOException $e) {
0 ignored issues
show
Bug introduced by
The class Ganga\Potato\PDOException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
93
            throw new PotatoException($e->getMessage());
94
        }
95
96
        return $db;
97
    }
98
99
    public function connectPostrges()
100
    {
101
102
    }
103
104 View Code Duplication
    public function connectMSSQL($config) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
105
        $this->checkParam($config);
106
107
        try {
108
            $db = new PDO("mssql:host=".$config['host'].";dbname=".$config['database'].", ".$config['user'].", ".$config['password']);
109
        } catch (PDOException $e) {
0 ignored issues
show
Bug introduced by
The class Ganga\Potato\PDOException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
110
            throw new PotatoException($e->getMessage());
111
        }
112
113
        return $db;
114
    }
115
116 View Code Duplication
    public function connectSQL($config) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
117
        $this->checkParam($config);
118
119
        try {
120
            $db = new PDO($config['type'].":host=".$config['host'].";dbname=".$config['database'].", ".$config['user'].", ".$config['password']);
121
        } catch (PDOException $e) {
0 ignored issues
show
Bug introduced by
The class Ganga\Potato\PDOException does not exist. Did you forget a USE statement, or did you not list all dependencies?

Scrutinizer analyzes your composer.json/composer.lock file if available to determine the classes, and functions that are defined by your dependencies.

It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.

Loading history...
122
            throw new PotatoException($e->getMessage());
123
        }
124
125
        return $db;
126
    }
127
}
128