Completed
Push — 2.0 ( bddf1c )
by Vermeulen
02:18
created

SqlConnect   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 128
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 0

Importance

Changes 0
Metric Value
wmc 9
lcom 2
cbo 0
dl 0
loc 128
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 18 2
A setUtf8() 0 4 1
A protect() 0 11 1
A getPDO() 0 4 1
A getConnectionInfos() 0 4 1
A getType() 0 4 1
A getNbQuery() 0 4 1
A upNbQuery() 0 4 1
1
<?php
2
3
namespace BfwSql;
4
5
/**
6
 * Class to initialize a connection to a SQL server with PDO
7
 * 
8
 * @package bfw-sql
9
 * @author Vermeulen Maxime <[email protected]>
10
 * @version 2.0
11
 */
12
class SqlConnect
13
{
14
    /**
15
     * @var \PDO $PDO PDO Connexion object
16
     */
17
    protected $PDO;
18
    
19
    /**
20
     * @var \stdClass $connectionInfos All informations about the connection
21
     */
22
    protected $connectionInfos;
23
    
24
    /**
25
     * @var string $type Connexion type (mysql/pgsql/etc)
26
     */
27
    protected $type;
28
    
29
    /**
30
     * @var integer $nbQuery (default 0) Number of request has been done
31
     */
32
    protected $nbQuery = 0;
33
    
34
    /**
35
     * Constructor
36
     * Initialize the connection
37
     * 
38
     * @param \stdClass $connectionInfos All informations about the connection
39
     * 
40
     * @throw \PDOException If Connexion fail
41
     */
42
    public function __construct($connectionInfos)
43
    {
44
        $this->connectionInfos = $connectionInfos;
45
        
46
        $host     = $connectionInfos->host;
47
        $baseName = $connectionInfos->baseName;
48
        
49
        $this->type = $connectionInfos->baseType;
50
        $this->PDO  = new \PDO(
51
            $this->type.':host='.$host.';dbname='.$baseName,
52
            $connectionInfos->user,
53
            $connectionInfos->password
54
        );
55
        
56
        if ($connectionInfos->useUTF8 === true) {
57
            $this->setUtf8($connectionInfos->useUTF8);
0 ignored issues
show
Unused Code introduced by
The call to SqlConnect::setUtf8() has too many arguments starting with $connectionInfos->useUTF8.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
58
        }
59
    }
60
    
61
    /**
62
     * Define charset to UTF-8 with mysql
63
     * 
64
     * @return void
65
     */
66
    protected function setUtf8()
67
    {
68
        $this->PDO->exec('SET NAMES utf8');
69
    }
70
    
71
    /**
72
     * Protect a data with the system implemented by the pdo drivers used.
73
     * 
74
     * @param string $string Data to protect
75
     * 
76
     * @return string
77
     */
78
    public function protect($string)
79
    {
80
        $protectedString = $this->PDO->quote($string);
81
        
82
        /**
83
         * The quote method add the caracter ' on the start and the end of the
84
         * protected string.
85
         * So we remove this quote at the start and the end of the string.
86
         */
87
        return substr($protectedString, 1, strlen($protectedString)-2);
88
    }
89
    
90
    /**
91
     * Getter to access at the property PDO
92
     * 
93
     * @return \PDO
94
     */
95
    public function getPDO()
96
    {
97
        return $this->PDO;
98
    }
99
    
100
    /**
101
     * Getter to access at the property connectionInfos
102
     * 
103
     * @return \stdClass
104
     */
105
    public function getConnectionInfos()
106
    {
107
        return $this->connectionInfos;
108
    }
109
    
110
    /**
111
     * Getter to access at the property type
112
     * 
113
     * @return string
114
     */
115
    public function getType() 
116
    {
117
        return $this->type;
118
    }
119
    
120
    /**
121
     * Getter to access at the property nbQuery
122
     * 
123
     * @return integer
124
     */
125
    public function getNbQuery()
126
    {
127
        return $this->nbQuery;
128
    }
129
    
130
    /**
131
     * Increment the number of query has been done
132
     * 
133
     * @return void
134
     */
135
    public function upNbQuery()
136
    {
137
        $this->nbQuery++;
138
    }
139
}
140