Completed
Push — 2.0 ( c11079...9c6898 )
by Vermeulen
02:10
created

SqlConnect::createConnection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 11
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
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
        $this->type            = $this->connectionInfos->baseType;
46
        
47
        $this->createConnection();
48
        
49
        if ($connectionInfos->useUTF8 === true) {
50
            $this->setUtf8();
51
        }
52
    }
53
    
54
    /**
55
     * Initialize the connection
56
     * 
57
     * @throw \PDOException If Connexion fail
58
     * 
59
     * @return void
60
     */
61
    protected function createConnection()
62
    {
63
        $host     = $this->connectionInfos->host;
64
        $baseName = $this->connectionInfos->baseName;
65
        
66
        $this->PDO  = new \PDO(
67
            $this->type.':host='.$host.';dbname='.$baseName,
68
            $this->connectionInfos->user,
69
            $this->connectionInfos->password
70
        );
71
    }
72
    
73
    /**
74
     * Define charset to UTF-8 with mysql
75
     * 
76
     * @return void
77
     */
78
    protected function setUtf8()
79
    {
80
        $this->PDO->exec('SET NAMES utf8');
81
    }
82
    
83
    /**
84
     * Protect a data with the system implemented by the pdo drivers used.
85
     * 
86
     * @param string $string Data to protect
87
     * 
88
     * @return string
89
     */
90
    public function protect($string)
91
    {
92
        $protectedString = $this->PDO->quote($string);
93
        
94
        /**
95
         * The quote method add the caracter ' on the start and the end of the
96
         * protected string.
97
         * So we remove this quote at the start and the end of the string.
98
         */
99
        return substr($protectedString, 1, strlen($protectedString) - 2);
100
    }
101
    
102
    /**
103
     * Getter to access at the property PDO
104
     * 
105
     * @return \PDO
106
     */
107
    public function getPDO()
108
    {
109
        return $this->PDO;
110
    }
111
    
112
    /**
113
     * Getter to access at the property connectionInfos
114
     * 
115
     * @return \stdClass
116
     */
117
    public function getConnectionInfos()
118
    {
119
        return $this->connectionInfos;
120
    }
121
    
122
    /**
123
     * Getter to access at the property type
124
     * 
125
     * @return string
126
     */
127
    public function getType() 
128
    {
129
        return $this->type;
130
    }
131
    
132
    /**
133
     * Getter to access at the property nbQuery
134
     * 
135
     * @return integer
136
     */
137
    public function getNbQuery()
138
    {
139
        return $this->nbQuery;
140
    }
141
    
142
    /**
143
     * Increment the number of query has been done
144
     * 
145
     * @return void
146
     */
147
    public function upNbQuery()
148
    {
149
        $this->nbQuery++;
150
    }
151
}
152