Passed
Push — 2.0 ( 43dc4b...8a9fec )
by Vermeulen
02:02
created

SqlConnect::mysqlUtf8()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace BfwSql;
4
5
use \Exception;
6
7
/**
8
 * Class to initialize a connection to a SQL server with PDO
9
 * 
10
 * @package bfw-sql
11
 * @author Vermeulen Maxime <[email protected]>
12
 * @version 2.0
13
 */
14
class SqlConnect
15
{
16
    /**
17
     * @const ERR_DSN_METHOD_NOT_FOUND Exception code if there is no method to
18
     * generate the dsn used by PDO.
19
     */
20
    const ERR_DSN_METHOD_NOT_FOUND = 2104001;
21
    
22
    /**
23
     * @var \PDO $PDO PDO Connexion object
24
     */
25
    protected $PDO;
26
    
27
    /**
28
     * @var \stdClass $connectionInfos All informations about the connection
29
     */
30
    protected $connectionInfos;
31
    
32
    /**
33
     * @var string $type Connexion type (mysql/pgsql/etc)
34
     */
35
    protected $type;
36
    
37
    /**
38
     * @var integer $nbQuery (default 0) Number of request has been done
39
     */
40
    protected $nbQuery = 0;
41
    
42
    /**
43
     * Constructor
44
     * Initialize the connection
45
     * 
46
     * @param object $connectionInfos All informations about the connection
47
     * 
48
     * @throw \PDOException If Connexion fail
49
     */
50
    public function __construct($connectionInfos)
51
    {
52
        $this->connectionInfos = $connectionInfos;
53
        $this->type            = &$this->connectionInfos->baseType;
54
    }
55
    
56
    /**
57
     * Initialize the connection
58
     * 
59
     * @throw \PDOException If Connexion fail
60
     * 
61
     * @return void
62
     */
63
    public function createConnection()
64
    {
65
        $usedClass          = \BfwSql\UsedClass::getInstance();
66
        $createDsnClassName = $usedClass->obtainClassNameToUse('CreatePdoDsn');
67
        $pdoClassName       = $usedClass->obtainClassNameToUse('PDO');
68
        
69
        if (!method_exists($createDsnClassName, $this->type)) {
70
            throw new Exception(
71
                'No method to generate DSN find on \BfwSql\CreatePdoDsn class.',
72
                self::ERR_DSN_METHOD_NOT_FOUND
73
            );
74
        }
75
        
76
        $this->PDO = new $pdoClassName(
77
            $createDsnClassName::{$this->type}($this->connectionInfos),
78
            $this->connectionInfos->user,
79
            $this->connectionInfos->password,
80
            $this->connectionInfos->pdoOptions
81
        );
82
            
83
        $this->pdoSetAttributes();
84
        
85
        if (!empty($this->connectionInfos->encoding)) {
86
            $this->sqlSetNames();
87
        }
88
    }
89
    
90
    /**
91
     * Define attributes for PDO.
92
     * List of attributes is define in base config on key pdoAttributes
93
     * 
94
     * @link http://php.net/manual/en/pdo.setattribute.php
95
     * 
96
     * @return void
97
     */
98
    protected function pdoSetAttributes()
99
    {
100
        $pdoAttributes = $this->connectionInfos->pdoAttributes;
101
        
102
        foreach ($pdoAttributes as $attributeName => $attributeValue) {
103
            $this->PDO->setAttribute($attributeName, $attributeValue);
104
        }
105
    }
106
    
107
    /**
108
     * Define charset to UTF-8 with mysql
109
     * 
110
     * @return void
111
     */
112
    protected function sqlSetNames()
113
    {
114
        $encoding = $this->connectionInfos->encoding;
115
        $this->PDO->exec('SET NAMES '.$encoding);
116
    }
117
    
118
    /**
119
     * Protect a data with the system implemented by the pdo drivers used.
120
     * 
121
     * @param string $datas Data to protect
122
     * 
123
     * @return string
124
     */
125
    public function protect(string $datas): string
126
    {
127
        $protectedString = $this->PDO->quote($datas);
128
        
129
        /**
130
         * The quote method add the caracter ' on the start and the end of the
131
         * protected string.
132
         * So we remove this quote at the start and the end of the string.
133
         */
134
        return substr($protectedString, 1, strlen($protectedString) - 2);
135
    }
136
    
137
    /**
138
     * Getter to access at the property PDO
139
     * 
140
     * @return \PDO
141
     */
142
    public function getPDO()
143
    {
144
        return $this->PDO;
145
    }
146
    
147
    /**
148
     * Getter to access at the property connectionInfos
149
     * 
150
     * @return object
151
     */
152
    public function getConnectionInfos()
153
    {
154
        return $this->connectionInfos;
155
    }
156
    
157
    /**
158
     * Getter to access at the property type
159
     * 
160
     * @return string
161
     */
162
    public function getType() : string
163
    {
164
        return $this->type;
165
    }
166
    
167
    /**
168
     * Getter to access at the property nbQuery
169
     * 
170
     * @return integer
171
     */
172
    public function getNbQuery(): int
173
    {
174
        return $this->nbQuery;
175
    }
176
    
177
    /**
178
     * Increment the number of query has been done
179
     * 
180
     * @return void
181
     */
182
    public function upNbQuery()
183
    {
184
        $this->nbQuery++;
185
    }
186
}
187